Ali Izadyar
Ali Izadyar

Reputation: 220

Can not connect to localhost by Volley

I am using Volley:1.1.1 to connect to localhost in my android application

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

The Logcat is showing this

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

-I have tried 10.0.2.2 and my IP address but the result is the same

Upvotes: 1

Views: 2294

Answers (1)

salmanwahed
salmanwahed

Reputation: 9647

Run your service in 0.0.0.0. It will make the server available in the same network. So if your computer's IP is 192.168.X.Y then you can access the service in:

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

Form more information I am Quoting from a SO Thread:

127.0.0.1 is normally the IP address assigned to the "loopback" or local-only interface. 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.

"localhost" is normally the hostname for the 127.0.0.1 IP address. It's usually set in /etc/hosts (or the Windows equivalent named "hosts" somewhere under %WINDIR%). You can use it just like any other hostname - try "ping localhost" to see how it resolves to 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". 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.

Upvotes: 1

Related Questions