Reputation: 3945
I'm using Volley in Android in order to make requests including fetching relatively large amount of data. I want to make timer of 5 seconds and if after it the request not returned - it probably means that there is slow connection and therefore I want to cancel the request. So far what I did:
Timer timer = new Timer();
VioozerVolleyRequestFactory mFactory = new VioozerVolleyRequestFactory(this);
RequestQueue mQueue = VioozerVolleySingleton.getInstance(this).getRequestQueue();
timer.schedule(new TimerTask() {
@Override
public void run() {
mQueue.cancelAll("MY_TAG");
}
}, 5000};
Request<String> request = mFactory.createRequest(RequestType,
new Listener<String>() {
@Override
public void onResponse(String response) {
timer.cancel();
//...
}
},
new ErrorListener<String>() {
@Override
public void onErrorResponse(String response) {
timer.cancel();
//...
}
}, extra);
request.setTag("MY_TAG");
mQueue.add(request);
My question: It seems that the request not canceled. The request is executed so the build in method cancelALL(TAG)
not relevant here. How can I still achieve my requirement?
Thanks,
Upvotes: 1
Views: 1427
Reputation: 136
By default Volley request timeout is set to 2500ms and it makes 1 retry per request.
You need to override DefaultRetryPolicy of Request.
for example:
Wait for 5000ms and do not perform any retry.
request.setRetryPolicy(new DefaultRetryPolicy(5000,0,1f));
Upvotes: 3