1

I am trying to do a GET request (StringRequest) with the Volley library. The file is on my wamp server (txt file). I keep getting a connection fail with my IP adress, and with localhost, and with 10.0.2.2.

There are 2 errors :

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

  • with my IP adress :

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

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);
}
yogidilip
  • 790
  • 6
  • 21
OrsCrous
  • 21
  • 2
  • 5

3 Answers3

0

first check that the api is reachable from your computer browser using your IP instead of localhost , if it is good . check again from a mobile browser connected on the same network wifi. Then don't ever use localhost and always use ips in your api url

Daniel Raouf
  • 2,307
  • 1
  • 22
  • 28
0

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. Just change 'localhost' to your address like '192.168...:8080' and make sure you add persmissions to androidmanifest

<uses-permission android:name="android.permission.INTERNET" />
Michał B
  • 62
  • 1
  • 6
0

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.

Which version of Wamp are you using? - I would suggest you to check the httpd.conf to allow access for anywhere like suggest in here

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);
}
Community
  • 1
  • 1
Igor Siqueira
  • 397
  • 3
  • 7