Skip to content
Advertisement

Why my http post request with android volley throws error (localhost)?

I’m working on an android studio project and i’ve had some problems trying to make a post request with volley library. I’ve already tested my API with postman and all works fine, so the problem is from the client part.

I’ve already added the internet permission in my android manifest:

<uses-permission android:name="android.permission.INTERNET"/>

Here is my code, i’ve created a function named “login” that receive two paramaters which are the data that i want to send by post method:

     private void login(final String email, final String password){
        final String url = "http://192.168.100.2:8000/login_facebook_app";

        RequestQueue requestQueue = Volley.newRequestQueue(this);

        StringRequest postRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Toast.makeText(getBaseContext(), response, Toast.LENGTH_SHORT).show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getBaseContext(), error.toString(), Toast.LENGTH_SHORT).show();
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String>  params = new HashMap<String, String>();
                params.put("email", email);
                params.put("password", password);

                return params;
            }
        };

        postRequest.setRetryPolicy(new DefaultRetryPolicy(
                15000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

        requestQueue.add(postRequest);
    }

I call that function on a click event login button, but when i click that button i get

com.android.volley.timeoutError

I’ve read many solutions and i’ve found that the timeout problem is due to firewall problems when the API is running in localhost. But when i disable the firewall throws me another error:

com.android.volley.NoConnectionError:java.net.ConnectionException: failed to connect to /192.168.100.2(port 8000) after 2500ms:isConnected failed: ECONNREFUSED(Connection refused)

I’ve tried many solutions of here and i cannot make work my app. Please help me xD

Advertisement

Answer

I’ve found the solution to the connectivity issues; i’d been trying to access from my cellphone to the url which was running in localhost on my computer through the IP where both, my cellphone and my computer are connected, althought this didn’t work for me because the request dies before reach to the server, so i tried another way, here’s the answer to access from your device to the localhost’s url running in your computer:

https://stackoverflow.com/a/53920173/11755598

Advertisement