Android: Volley NoConnectionError
Asked Answered
T

6

7

I'm trying to connect to a REST-Server (which I created with Spark) via Android. I can send POST-Requests with POSTMAN (Chrome Addon) and get what I want, but when I try to send the POST request from my Android device, I get the following error:

E/error: com.android.volley.NoConnectionError: java.net.ConnectException: failed to connect to /127.0.0.1 (port 4567) after 2500ms: isConnected failed: ECONNREFUSED (Connection refused)

This is the server part:

Spark.post("/up", new Route()
    {
        public Object handle(Request req, Response res)
        {
            System.out.println(req.queryParams());
            return "its something";
        }
    });

And this is the Android part:

public void sendHTTPRequest()
{
    RequestQueue MyRequestQueue = Volley.newRequestQueue(this);

    String url = "http://127.0.0.1:4567/up";
    StringRequest MyStringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            //This code is executed if the server responds, whether or not the response contains data.
            //The String 'response' contains the server's response.
        }
    }, new Response.ErrorListener() { //Create an error listener to handle errors appropriately.
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("error",error.toString());
        }
    }) {
        protected Map<String, String> getParams() {
            Map<String, String> MyData = new HashMap<String, String>();
            MyData.put("Field", "Value"); //Add the data you'd like to send to the server.
            return MyData;
        }
    };
    MyRequestQueue.add(MyStringRequest);
}

I also added the important permissions in the manifest file.

I searched for hours but I could not solve my problem :( I hope you can help me. Best regards, Felix

Transship answered 20/8, 2016 at 20:57 Comment(0)
M
17

TLDR; Use your server's wifi or ethernet IP address instead of 127.0.0.1

127.0.0.1 is the loopback IP address on your Android device. You need the IP address of your computer's NIC(network card). I presume you're running on an emulator or Android device of some sort. If your server is running on a local Wifi network, retrieve the IP address of your Wifi adapter or Ethernet Adapter then replace 127.0.0.1 with the said address.

Linux: ifconfig [wlan0]

Windows: ipconfig

Edit 2020:

On newer versions on Android, all HTTP traffic must be encrypted, otherwise the android framework will throw an error. Therefore it's advisable to use a tunnelling server like Ngrok - which comes with free SSL - when testing on your local server.

Madaih answered 20/8, 2016 at 21:4 Comment(4)
Hm, error: no route to host, when I use the IP address. Even with curl I cannot connect to the server.Transship
Try connecting to server on the machine via the machine's "localhost"Madaih
Yes, that worked (Postman, as I wrote, works), but when I try to send a POST request from my android device to the specific url (192.168.0.81:4567) I get no response. Maybe firewall problems? I checked the port with netstat, but my machine seems to listen on port 4567.Transship
Oh man. Thank you. This must be my best facepalm moment of the year. 127.0.0.1 is the emulated device's loopback.Gonsalve
H
5
Give Internet Permission:

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

Add this line in your manifest:

android:usesCleartextTraffic="true"

because I faced the same issue with my project.

It should look like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        ...
        android:usesCleartextTraffic="true"
        ...>
        ...
    </application>
</manifest>
Hydrazine answered 28/2, 2022 at 6:5 Comment(2)
This helped me so much tnanksAudible
this simple trick helps me. I was able to connect to digital ocean not lightsail. Now works!Anglaangle
U
1

if your test on local, try to check an ip address (on window open cmd run ipconfig) then put your ip address again.

This's an example

String url = http://127.0.0.1:8080/user <-- error

String url = "http://172.16.5.240:8080/user <-- worked

Unwish answered 8/8, 2019 at 13:13 Comment(0)
B
1

Android emulator uses 10.0.2.2 as a host loopback interface. Use it instead of 127.0.0.1 which is emulator itself.

Broadcasting answered 18/9, 2020 at 9:55 Comment(1)
Yes I tested using this and it works!!!!!Lambdacism
K
0

Check your xammp or wamp connection. if the mysql and apache services are not active then start all services and run your application it gave proper output

Kessinger answered 2/2, 2019 at 11:16 Comment(0)
D
-1

change http to https, this one solved my issue or android:usesCleartextTraffic="true" in manifest

Definitely answered 23/9, 2022 at 10:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.