我在Android Studio的Kotlin中遇到问题。HTTP请求不适用于我,我已经尝试了Fuel和Volley库。我已经在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>
中添加代码参数,则应在获取调用Api.retrofitService.get("200528-0961").enqueue( object: Callback<String>