Android:Volley NoConnectionError [英] Android: Volley NoConnectionError

查看:830
本文介绍了Android:Volley NoConnectionError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Android连接到REST-Server(我用Spark创建)

我可以使用POSTMAN(Chrome Addon)发送POST-Requests并获得我想要的
,但是当我尝试从我的Android
设备发送POST请求时,我收到以下错误:

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)

这是服务器部分:

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

这是Android部分:

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.

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

I searched for hours but I could not solve my problem :( I hope you can help me. Best regards, Felix

推荐答案

127.0.0.1 是Android设备上的环回地址。您需要计算机的IP地址。 (可能)正在收听 127.0.0.1 / localhost 。我假设您正在某种模拟器或Android设备上运行。如果您使用的是Wifi网络,使用 Wifi适配器的IP地址,然后将 127.0.0.1 替换为该地址。

127.0.0.1 is the loopback address on your android device. You need the IP address of your computer. Nothing(probably) is listening on 127.0.0.1 /localhost. I assume you 're running on an emulator or Android device of some sort. If your are on a Wifi network, use the IP address of your Wifi adapter then replace 127.0.0.1 with that address.

Linux: ifconfig [wlan0]

Windows使用: ipconfig

Windows use: ipconfig

这篇关于Android:Volley NoConnectionError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆