Android:Volley NoConnectionError

Cyb*_*der 3 android http-post android-volley

我正在尝试通过Android连接到REST服务器(我用Spark创建).我可以使用POSTMAN(Chrome Addon)发送POST-Requests并得到我想要的内容,但是当我尝试从我的Android设备发送POST请求时,我收到以下错误:

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)
Run Code Online (Sandbox Code Playgroud)

这是服务器部分:

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

这是Android的一部分:

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);
}
Run Code Online (Sandbox Code Playgroud)

我还在清单文件中添加了重要权限.

我搜索了几个小时,但我无法解决我的问题:(我希望你能帮助我.祝你好运,菲利克斯

pet*_*ula 8

127.0.0.1是Android设备上的环回地址.您需要计算机的IP地址.我认为你正在某种模拟器或Android设备上运行.如果您的服务器在本地Wifi网络上运行,请使用Wifi适配器的IP地址,然后将127.0.0.1替换为该地址.

TLDR; 使用服务器的IP地址而不是127.0.0.1

Linux的: ifconfig [wlan0]

视窗: ipconfig

  • 是的,这有效(邮递员,正如我所写的,有效),但是当我尝试从我的 android 设备向特定 url (192.168.0.81:4567) 发送 POST 请求时,我没有得到任何回应。也许防火墙问题?我用 netstat 检查了端口,但我的机器似乎在端口 4567 上侦听。 (2认同)