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

Cyberlander
  • 145
  • 1
  • 1
  • 11

5 Answers5

14

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.

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

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.

Peter Chaula
  • 3,094
  • 2
  • 22
  • 29
  • Hm, error: no route to host, when I use the IP address. Even with curl I cannot connect to the server. – Cyberlander Aug 20 '16 at 21:40
  • Try connecting to server on the machine via the machine's "**localhost**" – Peter Chaula Aug 20 '16 at 21:43
  • 2
    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. – Cyberlander Aug 20 '16 at 21:49
  • Oh man. Thank you. This must be my best facepalm moment of the year. 127.0.0.1 is the emulated device's loopback. – Eric M Apr 30 '21 at 20:26
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

Infomaster
  • 565
  • 4
  • 6
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.

Denys
  • 11
  • 2
1
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>
Mohidul
  • 11
  • 1
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