Android/kotlin/http通信
提供: 初心者エンジニアの簡易メモ
2020年2月14日 (金) 11:56時点におけるAdmin (トーク | 投稿記録)による版 (ページの作成:「==サンプル== HttpRequest.kt <pre> class HttpRequest { private val TAG = "HttpRequest" private var body = ""; fun request(requestUrl: String): Boolean {...」)
サンプル
HttpRequest.kt
class HttpRequest { private val TAG = "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 } }