I have been trying to figure out this issue for two days now and I am completly stumped. For some reason I am sending one request to the queue but volley is returning it tw
To stop the multiple request you can configure retry policy for your request object by using the setRetryPolicy() method of the request object. I managed to do this with following code:
req.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 0,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
With the above code, i reduced the timeout to 2 seconds and set the number of retries to 0.
Okay I faced the issue and came up with a solution. In my custom class i created a global variable on top as you can see in below code
MyRequest extends StringRequest {
private boolean mDelivered; //boolean to deliver response only once
then in constructor i initialsed it
public MyRequest(String url, Listener<String> listener,
ErrorListener errorListener, Context ctx) {
super(url, listener, errorListener);
mContext = ctx.getApplicationContext();
mDelivered = false;
DefaultRetryPolicy retryPolicy = new DefaultRetryPolicy(5000, 1, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
setRetryPolicy(retryPolicy);
}
and finally i overrided deliverResponse method. For first time mDelivered will be false and once it's delivered it'll be true. if response delivered from cache then cancel network request
@Override
protected void deliverResponse(String response) {
if (!mDelivered) {
mDelivered = true;
cancel(); //if cache delivers response better to cancel scheduled network request
super.deliverResponse(response);
}
}
Hope this helps
I believe I have figured it out after stepping through the code more. I believe it is the softttl that is causing this behavior. In the Volley cachedispatcher
if (!entry.refreshNeeded()) {
// Completely unexpired cache hit. Just deliver the response.
mDelivery.postResponse(request, response);
} else {
// Soft-expired cache hit. We can deliver the cached response,
// but we need to also send the request to the network for
// refreshing.
request.addMarker("cache-hit-refresh-needed");
request.setCacheEntry(entry);
// Mark the response as intermediate.
response.intermediate = true;
// Post the intermediate response back to the user and have
// the delivery then forward the request along to the network.
mDelivery.postResponse(request, response, new Runnable() {
@Override
public void run() {
try {
mNetworkQueue.put(request);
} catch (InterruptedException e) {
// Not much we can do about this.
}
}
});
This posts the response then sends it to the network which posts another response to the same listener.