android studio volley 网络请求
android studio volley web request
我正在尝试向 API returns android studio 上的 JSON 数组发出获取请求,但是当我检查日志时它说有问题...(顺便说一句,我用的是kotlin)
我做错了什么?
这是我的代码:
val url = "http://example.com"
val queue = Volley.newRequestQueue(context)
val jsonArrayRequest = JsonArrayRequest(Request.Method.GET, url, null,
{
Log.d("notification", "successful request!")
},
{
Log.d("notification", "error on request...")
})
queue.add(jsonArrayRequest)
}
var reqParam = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(userName, "UTF-8")
reqParam += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8")
val mURL = URL("<Yout API Link>?"+reqParam)
with(mURL.openConnection() as HttpURLConnection) {
// optional default is GET
requestMethod = "GET"
println("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()
}
it.close()
println("Response : $response")
}
}
试试这个,它会帮助解决你的问题..
看完Techno World的回答后我有了一个想法。
基本上这与 Techno World 所做的相同,但要容易得多。
我所做的是像这样打印对日志的响应:
val url = "http://example.com"
val queue = Volley.newRequestQueue(context)
val jsonArrayRequest = JsonArrayRequest(Request.Method.GET, url, null,
{
Log.d("notification", "successful request!")
},
{response ->
Log.d("notification", "error on request: $response")
})
queue.add(jsonArrayRequest)
}
所以当我查看日志时,我看到了这个:
error on request: com.android.volley.noconnectionerror: java.io.ioexception: cleartext http traffic to example.com not permitted
经过一番研究,我发现这是因为我使用了 http 而不是 https。所以我改变了它,它起作用了!
是stack overflow上关于那个错误的原始问题,还有更多的方法可以解决它...
我正在尝试向 API returns android studio 上的 JSON 数组发出获取请求,但是当我检查日志时它说有问题...(顺便说一句,我用的是kotlin)
我做错了什么?
这是我的代码:
val url = "http://example.com"
val queue = Volley.newRequestQueue(context)
val jsonArrayRequest = JsonArrayRequest(Request.Method.GET, url, null,
{
Log.d("notification", "successful request!")
},
{
Log.d("notification", "error on request...")
})
queue.add(jsonArrayRequest)
}
var reqParam = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(userName, "UTF-8")
reqParam += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8")
val mURL = URL("<Yout API Link>?"+reqParam)
with(mURL.openConnection() as HttpURLConnection) {
// optional default is GET
requestMethod = "GET"
println("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()
}
it.close()
println("Response : $response")
}
}
试试这个,它会帮助解决你的问题..
看完Techno World的回答后我有了一个想法。 基本上这与 Techno World 所做的相同,但要容易得多。 我所做的是像这样打印对日志的响应:
val url = "http://example.com"
val queue = Volley.newRequestQueue(context)
val jsonArrayRequest = JsonArrayRequest(Request.Method.GET, url, null,
{
Log.d("notification", "successful request!")
},
{response ->
Log.d("notification", "error on request: $response")
})
queue.add(jsonArrayRequest)
}
所以当我查看日志时,我看到了这个:
error on request: com.android.volley.noconnectionerror: java.io.ioexception: cleartext http traffic to example.com not permitted
经过一番研究,我发现这是因为我使用了 http 而不是 https。所以我改变了它,它起作用了!