【发布时间】:2018-12-24 02:41:08
【问题描述】:
我需要从我的Android/Kotlin 应用程序向后端REST API 发出请求。我需要为auth 发送JWT。我目前正在使用这样的代码,我无耻地从 answer 复制而来
private fun sendGet() {
val url = "http://www.google.com/"
val obj = URL(url)
with(obj.openConnection() as HttpURLConnection) {
// optional default is GET
requestMethod = "GET"
println("\nSending 'GET' request to URL : $url")
println("Response Code : $responseCode")
BufferedReader(InputStreamReader(inputStream)).use {
val response = StringBuffer()
var inputLine = it.readLine()
while (inputLine != null) {
response.append(inputLine)
inputLine = it.readLine()
}
println(response.toString())
}
}
}
但如果有更好的方法使用Android/Kotlin 中现成的设施,我不会接受此代码。
谁能给我看一些简单的代码,可以发出HTTP PUT 或GET 请求并在标头中包含JWT。
【问题讨论】: