0

Hi I am getting the Error response for the requests when the network is too slow even though the request is successfully send to the server? Could someone please help me to solve the issue?

somia
  • 611
  • 5
  • 22

3 Answers3

0

set value to response time out like

  stringRequest.setRetryPolicy(new RetryPolicy() {
        @Override
        public int getCurrentTimeout() {
            // Here goes the new timeout 3 minutes
            return 3*60*1000;
        }

        @Override
        public int getCurrentRetryCount() {
            // The max number of attempts
            return 5;
        }

        @Override
        public void retry(VolleyError error) throws VolleyError {

        }
    });

You should use above time out before two lines

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
Enamul Haque
  • 4,789
  • 1
  • 37
  • 50
0

Volley makes more than one call to same API, if the network is slow that gives misleading results. To avoid this scenario add setRetryPolicy flag before adding request to volley queue.

JsonObjectRequest jsonObjReq = new JsonObjectRequest(………….); 
jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(0,DefaultRetryPolicy.DEFAULT_MAX_RETRIES,DefaultRetryPolicy.DEFAULT.BACKOFF_MULT)); 
SingletonClass.getInstance.addToRequestQueue(jsonObjReq);
0

Try this code , it lets the api to wait for api response for some particular time period(provided by you)

// time provided to wait for api response
     jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(
                    10000,
                    DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

            // Adding request to request queue
            AppController.getInstance().addToRequestQueue(jsonObjReq, cancel_change_api);
        }
Android Geek
  • 8,956
  • 2
  • 21
  • 35