Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Volley NoConnectionError

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

like image 849
Cyberlander Avatar asked Aug 20 '16 20:08

Cyberlander


3 Answers

TLDR; Use your server's 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.

like image 164
Peter Chaula Avatar answered Oct 21 '22 03:10

Peter Chaula


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

like image 28
Infomaster Avatar answered Oct 21 '22 03:10

Infomaster


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>
like image 2
Mohidul Avatar answered Oct 21 '22 02:10

Mohidul