http 请求在 AndroidStudio 中的 Kotlin 上不起作用



我在 Android Studio 的 Kotlin 中遇到了问题。 HTTP请求对我不起作用,我已经尝试了燃料和凌空库。 我在AndroidManifest.xlm文件中添加了<uses-permission android:name="android.permission.INTERNET" />行。

凌空代码:

val queue = Volley.newRequestQueue(this)
val url = "http://drevo.kybernado.com/app/get_count.php?code=200528-0961"
val stringRequest = StringRequest(
Request.Method.GET, url,
Response.Listener<String> { response ->
Toast.makeText(this, "Response is: ${response.substring(0, 500)}", Toast.LENGTH_SHORT).show()
},
Response.ErrorListener {
Toast.makeText(this, "That didn't work!", Toast.LENGTH_SHORT).show()
})
queue.add(stringRequest)

燃油代号:

Fuel.get("http://drevo.kybernado.com/app/get_count.php?code=200528-0961")
.response { request, response, result ->
println(request)
println(response)
Toast.makeText(this, response.toString(), Toast.LENGTH_SHORT).show()
val (bytes, error) = result
Toast.makeText(this, result.toString(), Toast.LENGTH_SHORT).show()
if (bytes != null) {
println("[response bytes] ${String(bytes)}")
Toast.makeText(this, String(bytes), Toast.LENGTH_SHORT).show()
}
}

在凌空抽射中,它总是出现错误,而在 Fuel 中没有活动迹象 - 没有弹出烤面包机。 你知道它为什么会这样吗? 谢谢

PS:我是 Kotlin 和 Android Studio 的新手,但不是在编程方面。

最好使用这个没有协程的 RetrofitApi,其中你的BASE_URL是"http://drevo.kybernado.com/app/",你的GET_VALUE是"get_count.php",并在接口中添加一个代码参数fun get(@Query("code") code: String): Call<String>,你应该在 get 调用Api.retrofitService.get("200528-0961").enqueue( object: Callback<String>中传递这个参数

最新更新