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
127.0.0.1 is the loopback address on your android device. You need the IP address of your computer. I presume you 're running on an emulator or Android device of some sort. If your server is running on a local Wifi network, use the IP address of your Wifi adapter then replace 127.0.0.1 with that address.
TLDR; Use you server's IP address instead of 127.0.0.1
Linux:
ifconfig [wlan0]
Windows:
ipconfig
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