39
RequestQueue mRequestQueue = Volley.newRequestQueue(getApplicationContext());
mRequestQueue.add(new JsonObjectRequest(Method.GET, cityListUrl, null, new Listener<JSONObject>() 
{
    public void onResponse(JSONObject jsonResults) 
    {
        //Any Call
    }
}, new ErrorListener()
   {
        public void onErrorResponse(VolleyError arg0) 
        {
            //Any Error log
        }
   }
));

This is my Request Call and i want to change or set timeout for the request . Is it possible anyway ??

Aakash Goplani
  • 335
  • 1
  • 7
  • 17
Rizvan
  • 2,285
  • 2
  • 21
  • 42

5 Answers5

126

You should set the request's RetryPolicy:

myRequest.setRetryPolicy(new DefaultRetryPolicy(
                MY_SOCKET_TIMEOUT_MS, 
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, 
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

This would change your code to:

RequestQueue mRequestQueue = Volley.newRequestQueue(getApplicationContext());
JsonObjectRequest request = new JsonObjectRequest(Method.GET, cityListUrl, null, new
    Listener<JSONObject>() {
        public void onResponse(JSONObject jsonResults) {
            //Any Call
        }
    }, new ErrorListener(){
        public void onErrorResponse(VolleyError arg0) {
            //Any Error log
        }
    }
);


int socketTimeout = 30000;//30 seconds - change to what you want
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
request.setRetryPolicy(policy);
mRequestQueue.add(request);

If you are only just getting started with Volley, you might want to instead consider droidQuery, which is a little easier to configure:

int socketTimeout = 30000;
$.ajax(new AjaxOptions().url(cityListUrl)
                        .timeout(socketTimeout)
                        .success(new Function() {
                            public void invoke($ d, Object... args) {
                                JSONObject jsonResults = (JSONObject) args[0];
                                //Any call
                            }
                        })
                        .error(new Function() {
                            public void invoke($ d, Object... args) {
                                AjaxError error = (AjaxError) args[0];
                                Log.e("Ajax", error.toString());
                            }
                        }));
lase
  • 1,778
  • 1
  • 18
  • 33
Phil
  • 34,061
  • 21
  • 117
  • 154
  • Is there any onTimeOut method? – Dr.jacky Jul 03 '14 at 13:32
  • 1
    @Mr.Hyde I am not aware of one in *Volley*, but you can handle this in *droidQuery* by using *statusCode()* that accepts an array of status codes that can be returned, and a *Function*, so you can pass in timeout codes (480,419,504,503,522,598,599) and a *Function* that will handle these timeouts. The first argument of the *varargs* will be an *AjaxOptions* Object, with which you can optionally restart your request: `$.ajax((AjaxOptions)args[0]);` – Phil Jul 03 '14 at 15:12
  • I tried the same, ... some times it is working .... sometime it gives timeoutError and retries soon.... and again gives the same errors, ... How to identify and locate the actual cause??? ... – Bhuro Aug 31 '16 at 08:01
7

Something like this

RetryPolicy retryPolicy = new DefaultRetryPolicy(
    YOUR_TIMEOUT_MS,
    YOUT_MAX_RETRIES,
    YOUR_BACKOFF_MULT
);

JsonObjectRequest request = new JsonObjectRequest(...);
request.setRetryPolicy(retryPolicy);

Or you could implement your own RetryPolicy.

Blaz
  • 1,857
  • 14
  • 14
5

This is worked for me :

RequestQueue mRequestQueue = Volley.newRequestQueue(getApplicationContext());
JsonObjectRequest request = new JsonObjectRequest(Method.GET, cityListUrl, null, new
    Listener<JSONObject>() {
        public void onResponse(JSONObject jsonResults) {
            //Any Call
        }
    }, new ErrorListener(){
        public void onErrorResponse(VolleyError arg0) {
            //Any Error log
        }
    }
);


int socketTimeout = 30000;//30 seconds - change to what you want
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
request.setRetryPolicy(policy);
mRequestQueue.add(request);
King of Masses
  • 16,881
  • 4
  • 57
  • 75
  • android studio is a little bit buggy. but thanks for a minute or an hour of solving my problme.. gonna wait for new one ^_^ – david glorioso May 15 '16 at 08:42
1
void RequestVolley() {

    // Instantiate the RequestQuee
    RequestQueue queue = Volley.newRequestQueue(getApplication());

    //create new volley request
    JsonObjectRequest requestNew = new JsonObjectRequest(Request.Method.GET, Url, null, createMyReqSuccessListener(), createMyReqErrorListener());

    //Response.Listener and Error.Listener defined afterwards


    //first param is TIMEOUT ...integer
    //second param is number of retries ...integer
    //third is backoff multiplier ...integer

    requestNew.setRetryPolicy(new DefaultRetryPolicy(6000, 1, 1));

    queue.add(requestNew);
}

private Response.Listener < JSONObject > createMyReqSuccessListener() {
    return new Response.Listener < JSONObject > () {
        @Override
        public void onResponse(JSONObject response) {

            //do something
        }
    };
}

private Response.ErrorListener createMyReqErrorListener() {
    return new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

            //do something
        }
    };
}
Debosmit Ray
  • 4,731
  • 2
  • 21
  • 41
Aditya varale
  • 407
  • 7
  • 6
1
String url = "https://api.joind.in/v2.1/events?start=" + start + "&resultsperpage=20&format=json";
Log.i("DREG", "onLoadMoreItems: " + url);
final StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                // Add Code Here
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                    if (error instanceof NetworkError) {
                    } else if (error instanceof ServerError) {
                    } else if (error instanceof AuthFailureError) {
                    } else if (error instanceof ParseError) {
                    } else if (error instanceof NoConnectionError) {
                    } else if (error instanceof TimeoutError) {
                        Toast.makeText(getContext(),
                                "Oops. Timeout error!",
                                Toast.LENGTH_LONG).show();
                    }
            }
        }
);
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
        10000,
        DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
requestQueue.add(stringRequest);
Ahamadullah Saikat
  • 3,340
  • 30
  • 30