我是 android 开发的新手,我在使用我使用 asp.net 开发的简单 rest API 时遇到了一个问题,该 api 工作正常,我已经在 postman 中测试了 api,它也工作得很好。
这是网址:
http://localhost:5150/api/controller/1
Tthis url 在邮递员中工作正常。但是android给了我一个错误:无法连接到
localhost
,我尝试用我的IP地址替换localhost
例如:199.199.0.166
,网址就像http://199.199.0.166:5150/api/controller/1
。但是我无法在我的应用程序中使用它,这是 api 响应{"id":"1","name":"john","number":"123456"}
。现在我必须使用排球库在我的应用程序中显示这些名称和数字值。
val queue = Volley.newRequestQueue(activity)
val url = "http://199.199.0.166:5150/api/controller/1"
val jsonRequest = object : JsonObjectRequest(
Request.Method.GET,
url,
null,
Response.Listener {
val name = it.getString("name")
val number = it.getString("number")
username.text = name
phone.text = number
},
Response.ErrorListener{
Toast.makeText(activity,it.message.toString(),Toast.LENGTH_SHORT).show()
}
) {
override fun getHeaders(): MutableMap<String, String> {
val headers = HashMap<String,String>()
headers["Content-type"] = "application/json"
return headers
}
}
queue.add(jsonRequest)
这在启动应用程序后给我一个错误“null”,我也尝试了字符串请求但结果是一样的......