【发布时间】:2017-09-10 02:32:12
【问题描述】:
我正在尝试使用 Volley 库 发出 GET 请求 (StringRequest)。该文件位于我的 wamp 服务器(txt 文件)上。我的 IP 地址、localhost 和 10.0.2.2 的连接一直失败。
有2个错误:
- 与 localhost 和 10.0.2.2
java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 80) after 2500ms: isConnected failed: ECONNREFUSED (Connection refused)
- 我的IP地址:
java.net.ConnectException: failed to connect to /myIP (port 80) after 5000ms: isConnected failed: EHOSTUNREACH (No route to host)
我在 androidmanifest 上授予访问互联网的权限
这是我的代码:
public void volleyTest(Context ctx) {
RequestQueue queue = Volley.newRequestQueue(ctx);
String url ="http://localhost/file.txt";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("debug","Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("debug",error.getMessage());
}
});
queue.add(stringRequest);
}
【问题讨论】:
-
首先尝试在设备上的浏览器中打开您的 URL 以查看它是否有效。
-
我可以从设备上的浏览器读取txt文件(使用我的IP地址),但我仍然无法用Volley打开它
标签: java android http get android-volley