0

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

Abraham Arreola
  • 329
  • 4
  • 14
  • Connection refused indicates that the receiving machine is blocking the connection.There are several reasons for that. `192.168.100.2` is not `localhost`. It's typically `127.0.0.1` so would need more details on the service you're attempting to call. – Christopher Schneider Jul 08 '19 at 17:11
  • First check are you able to connet to local host by http or not as few device block http and only allow https. – Rahul Khatri Jul 08 '19 at 17:12
  • @ChristopherSchneider i know the ip is not localhost, but in android studio i run the program in my android device, that's the reason i can't use the typically localhost, so i use the ip where both, my pc and my android device are connected. I've already done in another project but with a sql connection. – Abraham Arreola Jul 08 '19 at 17:20
  • @RahulKhatri do you think is problem of my android device? how can i check that? I've programmed web pages with javascript, python and i've never had problem with my API requests, though i have with this. – Abraham Arreola Jul 08 '19 at 17:26
  • For Android device api >= 28 you can't use http only https permitted, in api >=23 it by default block http but with usesCleartextTraffic="true" in manifest you can use http url but still some devices don't allow like Xiomi device. But if there is CleartextTraffic issue then you will error CleartextTraffic not permited. – Rahul Khatri Jul 09 '19 at 06:47
  • You can do one thing try to check api in your android device Browser, it may help you. – Rahul Khatri Jul 09 '19 at 06:50

2 Answers2

1

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

Abraham Arreola
  • 329
  • 4
  • 14
0

Firstly, make sure that your server is running.

Then set usesCleartextTraffic to true in AndroidManifest file in application tag.

    <application
        .....
        android:usesCleartextTraffic="true"
     >
            ....
        </activity>

Then in url instead of "http://192.168.100.2:8000/login_facebook_app" use this "http://10.0.2.2:8000/login_facebook_app"

So now your new code is:

     private void login(final String email, final String password){
        final String url = "http://10.0.2.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));