facebook twitter hatena line email

「Android/kotlin/http通信」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(ページの作成:「==サンプル== HttpRequest.kt <pre> class HttpRequest { private val TAG = "HttpRequest" private var body = ""; fun request(requestUrl: String): Boolean {...」)
 
行3: 行3:
 
<pre>
 
<pre>
 
class HttpRequest {
 
class HttpRequest {
    private val TAG = "HttpRequest"
 
 
     private var body = "";
 
     private var body = "";
 
     fun request(requestUrl: String): Boolean {
 
     fun request(requestUrl: String): Boolean {
行25: 行24:
 
     fun getBody(): String {
 
     fun getBody(): String {
 
         return body
 
         return body
 +
    }
 +
    companion object {
 +
        private const val TAG = "HttpRequest"
 
     }
 
     }
 
}
 
}
 
</pre>
 
</pre>
 
参考:https://qiita.com/naoi/items/8df1409ad48ad8f3c632
 
参考:https://qiita.com/naoi/items/8df1409ad48ad8f3c632
 +
 +
==asynkでアクセス==
 +
<pre>
 +
import android.os.AsyncTask
 +
class HttpAsyncTask : AsyncTask<Void, Void, Boolean>() {
 +
    private val TAG = "HttpAsyncTask"
 +
    override fun doInBackground(vararg p: Void?): Boolean {
 +
        var httpRequest = HttpRequest()
 +
        //var http = HttpUtil()
 +
        var success = httpRequest.request("https://h2ch.com/?hogehoge")
 +
        Log.i(TAG, "body=" + httpRequest.getBody())
 +
        return false
 +
    }
 +
    override fun onPostExecute(result: Boolean?) {
 +
        super.onPostExecute(result)
 +
    }
 +
    companion object {
 +
        private const val TAG = "HttpAsyncTask"
 +
    }
 +
}
 +
</pre>

2020年2月14日 (金) 12:08時点における版

サンプル

HttpRequest.kt

class HttpRequest {
    private var body = "";
    fun request(requestUrl: String): Boolean {
        if (requestUrl.isEmpty()) {
            Log.w(TAG, "URLが空です。")
            return false
        }
        val url = URL(requestUrl)
        val urlConnection = url.openConnection() as HttpURLConnection
        urlConnection.requestMethod = "GET"
        urlConnection.connect()
        val br = BufferedReader(InputStreamReader(urlConnection.inputStream))
        val sb = StringBuilder()
        for (line: String? in br.readLines()) {
            line?.let { sb.append(line) }
        }
        br.close()
        body = sb.toString()
        return true
    }
    fun getBody(): String {
        return body
    }
    companion object {
        private const val TAG = "HttpRequest"
    }
}

参考:https://qiita.com/naoi/items/8df1409ad48ad8f3c632

asynkでアクセス

import android.os.AsyncTask
class HttpAsyncTask : AsyncTask<Void, Void, Boolean>() {
    private val TAG = "HttpAsyncTask"
    override fun doInBackground(vararg p: Void?): Boolean {
        var httpRequest = HttpRequest()
        //var http = HttpUtil()
        var success = httpRequest.request("https://h2ch.com/?hogehoge")
        Log.i(TAG, "body=" + httpRequest.getBody())
        return false
    }
    override fun onPostExecute(result: Boolean?) {
        super.onPostExecute(result)
    }
    companion object {
        private const val TAG = "HttpAsyncTask"
    }
}