「Android/kotlin/http通信」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→asyncでアクセス) |
(→asyncでアクセス) |
||
行36: | 行36: | ||
参考:https://qiita.com/naoi/items/8df1409ad48ad8f3c632 | 参考:https://qiita.com/naoi/items/8df1409ad48ad8f3c632 | ||
− | == | + | ==AsyncTsnkでアクセス== |
直アクセスだとアクセスできないのでAsyncTaskをかます。 | 直アクセスだとアクセスできないのでAsyncTaskをかます。 | ||
行59: | 行59: | ||
Log.i(TAG, "onPostExecute flag=" + flag) | Log.i(TAG, "onPostExecute flag=" + flag) | ||
super.onPostExecute(flag) | super.onPostExecute(flag) | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | ==コールチンでアクセス== | ||
+ | AsyncTaskの代わりにkotlin1.3より公式となったコールチンでもアクセスできます。 | ||
+ | |||
+ | |||
+ | Coroutines 正式 httprequest | ||
+ | |||
+ | </pre> | ||
+ | import androidx.appcompat.app.AppCompatActivity | ||
+ | import android.os.Bundle | ||
+ | import android.util.Log | ||
+ | import kotlinx.android.synthetic.main.activity_main.* | ||
+ | import kotlinx.coroutines.CoroutineScope | ||
+ | import kotlinx.coroutines.Dispatchers | ||
+ | import kotlinx.coroutines.launch | ||
+ | import kotlinx.coroutines.withContext | ||
+ | |||
+ | |||
+ | class MainActivity : AppCompatActivity() { | ||
+ | val scope = CoroutineScope(Dispatchers.Default) | ||
+ | override fun onCreate(savedInstanceState: Bundle?) { | ||
+ | super.onCreate(savedInstanceState) | ||
+ | setContentView(R.layout.activity_main) | ||
+ | scope.launch { | ||
+ | httpTask() | ||
+ | } | ||
+ | } | ||
+ | private suspend fun httpTask() { | ||
+ | try { | ||
+ | withContext(Dispatchers.Main) { | ||
+ | Log.i(localClassName, "onPreExecute") | ||
+ | } | ||
+ | var httpRequest = HttpRequest() | ||
+ | //var http = HttpUtil() | ||
+ | var success: Boolean = httpRequest.request("https://h2ch.com/?hogehoge") | ||
+ | Log.i(localClassName, "body=" + httpRequest.getBody()) | ||
+ | |||
+ | withContext(Dispatchers.Main) { | ||
+ | Log.i(localClassName, "onPostExecute") | ||
+ | } | ||
+ | } catch (e: Exception) { | ||
+ | Log.e(localClassName, "onCancelled", e) | ||
+ | } | ||
} | } | ||
} | } | ||
</pre> | </pre> |
2020年2月14日 (金) 14:23時点における版
準備
AndroidManifext.xmlに以下を追加しておく。
<uses-permission android:name="android.permission.INTERNET" />
サンプル
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
AsyncTsnkでアクセス
直アクセスだとアクセスできないのでAsyncTaskをかます。
起動方法
var task = HttpAsyncTask(); task.execute();
HttpAsyncTask.kt
import android.os.AsyncTask class HttpAsyncTask : AsyncTask<Void, Void, Boolean>() { override fun onPreExecute() { Log.i(TAG, "onPreExecute") } override fun doInBackground(vararg p: Void?): Boolean { var httpRequest = HttpRequest() var success: Boolean = httpRequest.request("https://example.com/?hogehoge") Log.i(TAG, "body=" + httpRequest.getBody()) return true } override fun onPostExecute(flag: Boolean) { Log.i(TAG, "onPostExecute flag=" + flag) super.onPostExecute(flag) } }
コールチンでアクセス
AsyncTaskの代わりにkotlin1.3より公式となったコールチンでもアクセスできます。
Coroutines 正式 httprequest
</pre> import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.util.Log import kotlinx.android.synthetic.main.activity_main.* import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext
class MainActivity : AppCompatActivity() {
val scope = CoroutineScope(Dispatchers.Default) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) scope.launch { httpTask() } } private suspend fun httpTask() { try { withContext(Dispatchers.Main) { Log.i(localClassName, "onPreExecute") } var httpRequest = HttpRequest() //var http = HttpUtil() var success: Boolean = httpRequest.request("https://h2ch.com/?hogehoge") Log.i(localClassName, "body=" + httpRequest.getBody())
withContext(Dispatchers.Main) { Log.i(localClassName, "onPostExecute") } } catch (e: Exception) { Log.e(localClassName, "onCancelled", e) } }
} </pre>