【发布时间】:2019-11-18 02:31:00
【问题描述】:
我准备了一个节点 js 网络服务,当我使用以下 URL 测试它时返回 JSON 响应:http://localhost:3000/users?name=ali&password=ali,它成功返回了我设置的 JSON 响应,因此我确保它正常工作。 实际上,我正在尝试在 android 中使用此 web 服务,所以我尝试在 android 中使用 Volley 库获取 JSON 响应,但我得到:com.android.volley.TimeoutError 这是我正在建立连接并发出请求的 android 代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String URL = "http://196.2.182.69:3000/users";
RequestQueue requestQueue = Volley.newRequestQueue(this);
JsonObjectRequest objectRequest = new JsonObjectRequest(
Request.Method.GET,
URL,
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.e("Rest Response",response.toString());
System.out.println(response.toString());
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Rest Response",error.toString());
}
}
);
requestQueue.add(objectRequest);
}
注意:我用我的 IP 地址更改了 'localhost' 并停用了我的防火墙
【问题讨论】:
-
您的 Apache 服务器是否已启动并正在运行?您是否使用邮递员收到回复?
-
是的 apache 正在运行,我正在从数据库表中获取查询结果,因为我现在看到的问题是关于从 android 模拟器连接到 localhost
-
你的模拟器应该和你的 localhost API 在同一个网络上。
标签: java android node.js json android-volley