Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 ??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
220 views
Welcome To Ask or Share your Answers For Others

1 Answer

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());
                            }
                        }));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...