1 package jp.sfjp.gokigen.a01c.utils
3 import android.graphics.Bitmap
4 import android.graphics.BitmapFactory
5 import android.util.Log
7 import java.net.HttpURLConnection
10 class SimpleHttpClient
17 fun httpGet(url: String, timeoutMs: Int): String
19 var inputStream : InputStream? = null
21 var timeout = timeoutMs
24 timeout = DEFAULT_TIMEOUT
27 // HTTP GETメソッドで要求を投げる
30 val httpConn = URL(url).openConnection() as HttpURLConnection
33 httpConn.requestMethod = "GET"
34 httpConn.connectTimeout = timeout
35 httpConn.readTimeout = timeout
37 val responseCode = httpConn.responseCode
38 if (responseCode == HttpURLConnection.HTTP_OK)
40 inputStream = httpConn.inputStream
42 if (inputStream == null)
44 Log.w(TAG, "httpGet: Response Code Error: $responseCode: $url")
48 catch (ee : Exception)
50 Log.w(TAG, "httpGet: " + url + " " + ee.message)
58 Log.w(TAG, "httpGet(2): " + url + " " + e.message)
66 val responseBuf = StringBuilder()
67 val reader = BufferedReader(InputStreamReader(inputStream))
69 while (reader.read().also { c = it } != -1)
71 responseBuf.append(c.toChar())
73 replyString = responseBuf.toString()
78 Log.w(TAG, "httpGet: exception: " + e.message)
100 fun httpGetBytes(url: String, setProperty: Map<String, String>?, timeoutMs: Int, callback: IReceivedMessageCallback)
102 httpCommandBytes(url, "GET", null, setProperty, null, timeoutMs, callback)
110 fun httpPostBytes(url: String, postData: String?, setProperty: Map<String, String>?, timeoutMs: Int, callback: IReceivedMessageCallback)
112 httpCommandBytes(url, "POST", postData, setProperty, null, timeoutMs, callback)
115 private fun httpCommandBytes(url: String, requestMethod: String, postData: String?, setProperty: Map<String, String>?, contentType: String?, timeoutMs: Int, callback: IReceivedMessageCallback)
117 var inputStream: InputStream? = null
118 var timeout = timeoutMs
121 timeout = DEFAULT_TIMEOUT
127 val httpConn = URL(url).openConnection() as HttpURLConnection
128 httpConn.requestMethod = requestMethod
129 if (setProperty != null)
131 for (key in setProperty.keys)
133 val value = setProperty[key]
134 httpConn.setRequestProperty(key, value)
137 if (contentType != null)
139 httpConn.setRequestProperty("Content-Type", contentType)
141 httpConn.connectTimeout = timeout
142 httpConn.readTimeout = timeout
143 if (postData == null)
149 httpConn.doInput = true
150 httpConn.doOutput = true
151 val outputStream = httpConn.outputStream
152 val writer = OutputStreamWriter(outputStream, "UTF-8")
153 writer.write(postData)
158 val responseCode = httpConn.responseCode
159 if (responseCode == HttpURLConnection.HTTP_OK)
161 inputStream = httpConn.inputStream
163 if (inputStream == null)
165 Log.w(TAG, " http $requestMethod Response Code Error: $responseCode: $url")
166 callback.onErrorOccurred(NullPointerException())
167 callback.onCompleted()
174 var contentLength = httpConn.contentLength
175 if (contentLength < 0)
177 // コンテンツ長が取れない場合の処理...
180 val headers = httpConn.headerFields
181 // コンテンツ長さが取れない場合は、HTTP応答ヘッダから取得する
182 val valueList = headers["X-FILE_SIZE"]
185 if (valueList != null)
187 contentLength = getValue(valueList).toInt()
190 catch (ee: Exception)
200 val buffer = ByteArray(BUFFER_SIZE)
202 var readSize = inputStream.read(buffer, 0, BUFFER_SIZE)
203 while (readSize != -1)
205 callback.onReceive(readBytes, contentLength, readSize, buffer)
206 readBytes += readSize
207 readSize = inputStream.read(buffer, 0, BUFFER_SIZE)
209 Log.v(TAG, "RECEIVED $readBytes BYTES. (contentLength : $contentLength)")
214 Log.w(TAG, "httpGet: exception: " + e.message)
216 callback.onErrorOccurred(e)
232 Log.w(TAG, "http " + requestMethod + " " + url + " " + e.message)
234 callback.onErrorOccurred(e)
235 callback.onCompleted()
238 callback.onCompleted()
242 private fun getValue(valueList: List<String>): String
246 val values = StringBuilder()
247 for (value in valueList)
259 return values.toString()
262 fun httpGetBitmap(url: String, setProperty: Map<String, String>?, timeoutMs: Int): Bitmap?
264 return (httpCommandBitmap(url, "GET", null, setProperty, null, timeoutMs))
272 fun httpPostBitmap(url: String, postData: String?, timeoutMs: Int): Bitmap?
274 return (httpCommandBitmap(url, "POST", postData, null, null, timeoutMs))
282 private fun httpCommandBitmap(url: String, requestMethod: String, postData: String?, setProperty: Map<String, String>?, contentType: String?, timeoutMs: Int): Bitmap?
284 //var httpConn: HttpURLConnection? = null
285 var inputStream: InputStream? = null
286 //var outputStream: OutputStream? = null
287 //var writer: OutputStreamWriter? = null
288 var bmp: Bitmap? = null
289 var timeout = timeoutMs
292 timeout = DEFAULT_TIMEOUT
298 val httpConn = URL(url).openConnection() as HttpURLConnection
299 httpConn.requestMethod = requestMethod
300 if (setProperty != null)
302 for (key in setProperty.keys)
304 val value = setProperty[key]
305 httpConn.setRequestProperty(key, value)
308 if (contentType != null)
310 httpConn.setRequestProperty("Content-Type", contentType)
312 httpConn.connectTimeout = timeout
313 httpConn.readTimeout = timeout
314 if (postData == null)
320 httpConn.doInput = true
321 httpConn.doOutput = true
322 val outputStream = httpConn.outputStream
323 val writer = OutputStreamWriter(outputStream, "UTF-8")
324 writer.write(postData)
329 val responseCode = httpConn.responseCode
330 if (responseCode == HttpURLConnection.HTTP_OK)
332 inputStream = httpConn.inputStream
333 if (inputStream != null)
335 bmp = BitmapFactory.decodeStream(inputStream)
338 if (inputStream == null)
340 Log.w(TAG, "http: ($requestMethod) Response Code Error: $responseCode: $url")
347 Log.w(TAG, "http: (" + requestMethod + ") " + url + " " + e.message)
359 fun httpPost(url: String, postData: String?, timeoutMs: Int): String?
361 return (httpCommand(url, "POST", postData, null, null, timeoutMs))
369 fun httpGetWithHeader(url: String, headerMap: Map<String, String>?, contentType: String?, timeoutMs: Int): String?
371 return (httpCommand(url, "GET", null, headerMap, contentType, timeoutMs))
379 fun httpPostWithHeader(url: String, postData: String?, headerMap: Map<String, String>?, contentType: String?, timeoutMs: Int): String?
381 return (httpCommand(url, "POST", postData, headerMap, contentType, timeoutMs))
389 fun httpPutWithHeader(url: String, putData: String?, headerMap: Map<String, String>?, contentType: String?, timeoutMs: Int): String?
391 return (httpCommand(url, "PUT", putData, headerMap, contentType, timeoutMs))
399 fun httpPut(url: String, postData: String?, timeoutMs: Int): String?
401 return (httpCommand(url, "PUT", postData, null, null, timeoutMs))
409 fun httpOptions(url: String, optionsData: String?, timeoutMs: Int): String?
411 return (httpCommand(url, "OPTIONS", optionsData, null, null, timeoutMs))
419 private fun httpCommand(url: String, requestMethod: String, postData: String?, setProperty: Map<String, String>?, contentType: String?, timeoutMs: Int): String?
421 var inputStream: InputStream? = null
422 var timeout = timeoutMs
425 timeout = DEFAULT_TIMEOUT
431 val httpConn = URL(url).openConnection() as HttpURLConnection
432 httpConn.requestMethod = requestMethod
433 if (setProperty != null)
435 for (key in setProperty.keys)
437 val value = setProperty[key]
438 httpConn.setRequestProperty(key, value)
441 if (contentType != null)
443 httpConn.setRequestProperty("Content-Type", contentType)
445 httpConn.connectTimeout = timeout
446 httpConn.readTimeout = timeout
447 if (postData == null)
453 httpConn.doInput = true
454 httpConn.doOutput = true
455 val outputStream = httpConn.outputStream
456 val writer = OutputStreamWriter(outputStream, "UTF-8")
457 writer.write(postData)
462 val responseCode = httpConn.responseCode
463 if (responseCode == HttpURLConnection.HTTP_OK)
465 inputStream = httpConn.inputStream
467 if (inputStream == null)
469 Log.w(TAG, "http $requestMethod : Response Code Error: $responseCode: $url")
475 Log.w(TAG, "http " + requestMethod + " : IOException: " + e.message)
481 return readFromInputStream(inputStream)
484 private fun readFromInputStream(inputStream: InputStream?): String
486 //var reader: BufferedReader? = null
488 if (inputStream == null)
494 val responseBuf = StringBuilder()
495 val reader = BufferedReader(InputStreamReader(inputStream))
497 while (reader.read().also { c = it } != -1)
499 responseBuf.append(c.toChar())
501 replyString = responseBuf.toString()
511 interface IReceivedMessageCallback
514 fun onErrorOccurred(e: Exception?)
515 fun onReceive(readBytes: Int, length: Int, size: Int, data: ByteArray?)
520 private val TAG = SimpleHttpClient::class.java.simpleName
521 private const val DEFAULT_TIMEOUT = 10 * 1000 // [ms]
522 private const val BUFFER_SIZE = 131072 * 2 // 256kB