【问题标题】:Android Studio server connection failedAndroid Studio 服务器连接失败
【发布时间】:2017-09-10 02:32:12
【问题描述】:

我正在尝试使用 Volley 库 发出 GET 请求 (StringRequest)。该文件位于我的 wamp 服务器(txt 文件)上。我的 IP 地址localhost10.0.2.2 的连接一直失败。

有2个错误:

  • localhost10.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


【解决方案1】:

首先检查 api 是否可以从您的计算机浏览器使用您的 IP 而不是 localhost 访问,如果它是好的。从连接在同一网络 wifi 上的移动浏览器再次检查。然后永远不要使用 localhost 并始终在您的 api url 中使用 ips

【讨论】:

    【解决方案2】:

    您需要使用端口号获取机器的 IP 地址(使用 ipconfig),并在字符串 url 中使用它而不是 'localhost'。 只需将 'localhost' 更改为您的地址,例如 '192.168...:8080' 并确保将权限添加到 androidmanifest

    <uses-permission android:name="android.permission.INTERNET" />
    

    【讨论】:

      【解决方案3】:

      我用相同的设置在我的机器上运行了你的代码, 从 android/volley 部分来看,一切都很好,我可以使用以下代码访问我的文件,它基本上是你的,只需稍作修改。

      您使用的是哪个版本的 Wamp? - 我建议你检查 httpd.conf 以允许访问任何地方,如here 中的建议

      我在我的机器上运行的代码,如果有帮助的话:

        public void volleyTest(Context ctx) {
      
          RequestQueue queue = Volley.newRequestQueue(ctx);
          String url = "http://192.168.15.28/file.txt";
      
          com.android.volley.Response.Listener<String> listender = new com.android.volley.Response.Listener<String>() {
              @Override
              public void onResponse(String response) {
                  System.out.println(response);
              }
          };
          com.android.volley.Response.ErrorListener error = new com.android.volley.Response.ErrorListener() {
              @Override
              public void onErrorResponse(VolleyError error) {
                  System.out.println(error.getLocalizedMessage());
              }
          };
      
          StringRequest t = new StringRequest(Request.Method.GET, url, listender, error);
          queue.add(t);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-09
        • 2016-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多