简体   繁体   English

无法通过 Volley 连接到本地主机

[英]Can not connect to localhost by Volley

I am using Volley:1.1.1 to connect to localhost in my android application我正在使用Volley:1.1.1在我的 android 应用程序中连接到本地主机

my code :我的代码:

public static final String API_URL = "http://127.0.0.1:5365/api/Account/LoginUser?username=A_easy&password=123";

JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, API_URL, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.i("APIRes", "response : " + response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.i("APIRes", "error : " + error.getMessage());
            }
        });
        RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
        queue.add(request);

The error I get is error:null我得到的错误error:null

The Logcat is showing this Logcat 正在显示这个

I/Choreographer: Skipped 170 frames!  The application may be doing too much work on its main thread.
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
I/APIRes: error : null

-It works on Postman - 它适用于邮递员

-I have tried GET method but the result is the same - 我尝试过GET方法,但结果是一样的

-I have tried 10.0.2.2 and my IP address but the result is the same - 我试过10.0.2.2和我的IP 地址但结果是一样的

Run your service in 0.0.0.0 .0.0.0.0运行您的服务。 It will make the server available in the same network.它将使服务器在同一网络中可用。 So if your computer's IP is 192.168.XY then you can access the service in:因此,如果您计算机的 IP 是192.168.XY那么您可以通过以下方式访问该服务:

http://192.168.X.Y:5365/api/Account/LoginUser?username=A_easy&password=123

Form more information I am Quoting from a SO Thread :形成更多信息我引用自SO Thread

127.0.0.1 is normally the IP address assigned to the "loopback" or local-only interface. 127.0.0.1 通常是分配给“环回”或仅本地接口的 IP 地址。 This is a "fake" network adapter that can only communicate within the same host.这是一个“假”网络适配器,只能在同一主机内进行通信。 It's often used when you want a network-capable application to only serve clients on the same host.当您希望具有网络功能的应用程序仅为同一主机上的客户端提供服务时,通常会使用它。 A process that is listening on 127.0.0.1 for connections will only receive local connections on that socket.在 127.0.0.1 上侦听连接的进程将仅接收该套接字上的本地连接。

"localhost" is normally the hostname for the 127.0.0.1 IP address. “localhost”通常是 127.0.0.1 IP 地址的主机名。 It's usually set in /etc/hosts (or the Windows equivalent named "hosts" somewhere under %WINDIR%).它通常设置在 /etc/hosts(或 %WINDIR% 下某处名为“hosts”的 Windows 等效项中)。 You can use it just like any other hostname - try "ping localhost" to see how it resolves to 127.0.0.1.您可以像使用任何其他主机名一样使用它 - 尝试“ping localhost”以查看它如何解析为 127.0.0.1。

0.0.0.0 has a couple of different meanings, but in this context, when a server is told to listen on 0.0.0.0 that means "listen on every available network interface". 0.0.0.0 有几个不同的含义,但在这种情况下,当服务器被告知要侦听 0.0.0.0 时,这意味着“侦听每个可用的网络接口”。 The loopback adapter with IP address 127.0.0.1 from the perspective of the server process looks just like any other network adapter on the machine, so a server told to listen on 0.0.0.0 will accept connections on that interface too.从服务器进程的角度来看,IP 地址为 127.0.0.1 的环回适配器看起来就像机器上的任何其他网络适配器,因此被告知侦听 0.0.0.0 的服务器也将接受该接口上的连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM