简体   繁体   中英

Android device giving timeout error when making http request

I am getting a timeout error on my request in my android app. I have set the retry policy but it did not solve the issue. When tested on my emulator it works fine with no error, but when using a real device to test it gives the timeout error.

public void makeRequest(final String user, final String cred)
    {
        String url = "http://10.0.2.2:8888/map/api/login";

        StringRequest postRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>()
                {
                    @Override
                    public void onResponse(String response)
                    {
                        try
                        {
                            JSONObject jsonResponse = new JSONObject(response);
                            String status = jsonResponse.getString("status");
                            String token = jsonResponse.getString("token");


                            if(status.equalsIgnoreCase("error"))
                            {
                                Snackbar.make(findViewById(R.id.myCoordinatorLayout), jsonResponse.getString("message"), Snackbar.LENGTH_LONG).show();
                            }
                            else if (status.equalsIgnoreCase("success"))
                            {
                                System.out.println(jsonResponse);
                                Intent loader = new Intent(home.this,webViewActivity.class);
                                loader.putExtra(EXTRA_MESSAGE,token);
                                startActivity(loader);
                            }

                        }
                        catch (JSONException e)
                        {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener()
                {
                    @Override
                    public void onErrorResponse(VolleyError error)
                    {
                        error.printStackTrace();
                    }
                }
        ){
            @Override
            protected Map<String, String> getParams()
            {
                Map<String, String> params = new HashMap<>();
                params.put("portal[username]", user);
                params.put("portal[password]", cred);
                params.put("portal[From]","web");
                return params;
            }
        };
        postRequest.setRetryPolicy(new DefaultRetryPolicy(
                7000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        Volley.newRequestQueue(getApplicationContext()).add(postRequest);


    }

The error i get is below

08-18 12:42:46.341 16112-16112/com.mobile.map.map_mobile W/System.err: com.android.volley.TimeoutError
08-18 12:42:46.341 16112-16112/com.mobile.map.map_mobile W/System.err:     at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:141)
08-18 12:42:46.341 16112-16112/com.mobile.map.map_mobile W/System.err:     at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)

Can you check on the different device and on the different network. If it doesn't work please check the response time in postman if it is more than 2500 millisecond(volley default timeout), increase the volley's default timeout in DefaultRetryPolicy.class

I am assuming that your Key value pair is for Body.

        JSONObject params = new JSONObject();

            try {
                params.put("portal[username]", user);
                params.put("portal[password]", cred);
                params.put("portal[From]","web");
            } catch (JSONException e) {
               // Do something
            }

        Response.Listener<JSONObject> jsonObjectListener = new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    // Response here
                }
            };

        Response.ErrorListener errorListener = new  Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // Error here
                }
            };

       String url = "http://10.0.2.2:8888/map/api/login";

       JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, url, params,
                    jsonObjectListener, errorListener);

       Volley.newRequestQueue(getApplicationContext()).add(jsonRequest);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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