Android: Volley NoConnectionError

血红的双手。 提交于 2019-12-01 06:01:22

问题


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


回答1:


127.0.0.1 is the loopback ip 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




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/39058638/android-volley-noconnectionerror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!