简体   繁体   English

Android Studio服务器连接失败

[英]Android Studio server connection failed

I am trying to do a GET request (StringRequest) with the Volley library . 我正在尝试使用Volley库执行GET请求(StringRequest)。 The file is on my wamp server (txt file). 该文件位于我的wamp服务器(txt文件)上。 I keep getting a connection fail with my IP adress , and with localhost , and with 10.0.2.2 . 我的IP地址localhost以及10.0.2.2的连接失败。

There are 2 errors : 有2个错误:

  • with localhost and 10.0.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)

  • with my IP adress : 与我的IP地址

java.net.ConnectException: failed to connect to /myIP (port 80) after 5000ms: isConnected failed: EHOSTUNREACH (No route to host)

I gave permissions to access internet on the androidmanifest 我给了androidmanifest访问互联网的权限

Here is my code : 这是我的代码:

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);
}

first check that the api is reachable from your computer browser using your IP instead of localhost , if it is good . 首先检查api是否可以使用您的IP而不是localhost从您的计算机浏览器访问,如果它是好的。 check again from a mobile browser connected on the same network wifi. 再次从连接在同一网络wifi上的移动浏览器中检查。 Then don't ever use localhost and always use ips in your api url 然后不要使用localhost并始终在您的api url中使用ips

You need to get the IP address of your machine (use ipconfig) with port number and use that instead of 'localhost' in your String url. 您需要获取具有端口号的计算机的IP地址(使用ipconfig),并在String url中使用它而不是“localhost”。 Just change 'localhost' to your address like '192.168...:8080' and make sure you add persmissions to androidmanifest 只需将'localhost'更改为您的地址,如'192.168 ...:8080',并确保您向androidmanifest添加持久性

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

I ran your code in my machine with the same setup, from the android/volley part everything is fine, I was able to access my file with the following code, its basically yours with some minor modification. 我使用相同的设置在我的机器上运行你的代码,从android / volley部分一切都很好,我能够使用以下代码访问我的文件,它基本上是你的一些小修改。

Which version of Wamp are you using? 您使用的是哪个版本的Wamp? - I would suggest you to check the httpd.conf to allow access for anywhere like suggest in here - 我建议你查看httpd.conf以允许访问此处的建议

the code that I ran in my machine, if helps: 我在我的机器上运行的代码,如果有帮助:

  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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM