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 twice, which is calling the listener twice and doubling the results in my listview. I turned on the logging for Volley and I can see the request being added to the queue and returned, then a few seconds later the same request is also returned. Log below
V/Volley(14666): [188] CacheDispatcher.run: start new dispatcher
11-15 12:29:30.152: V/Volley(14666): [1] RequestQueue.add: Request for cacheKey=http://reallylongurl is in flight, putting on hold.
11-15 12:29:39.722: V/Volley(14666): [1] RequestQueue.finish: Releasing 1 waiting requests for cacheKey=http://reallylongurl.
11-15 12:29:39.722: D/Volley(14666): [1] MarkerLog.finish: (9809 ms) [ ] http://reallylongurl 0xd68d6603 NORMAL 1
11-15 12:29:39.732: D/Volley(14666): [1] MarkerLog.finish: (+0 ) [ 1] add-to-queue
11-15 12:29:39.732: D/Volley(14666): [1] MarkerLog.finish: (+2169) [188] cache-queue-take
11-15 12:29:39.742: D/Volley(14666): [1] MarkerLog.finish: (+37 ) [188] cache-hit
11-15 12:29:39.742: D/Volley(14666): [1] MarkerLog.finish: (+6878) [188] cache-hit-parsed
11-15 12:29:39.742: D/Volley(14666): [1] MarkerLog.finish: (+0 ) [188] post-response
11-15 12:29:39.752: D/Volley(14666): [1] MarkerLog.finish: (+725 ) [ 1] done
A few other requests get queued here.
11-15 12:29:48.405: D/Volley(14666): [1] MarkerLog.finish: (18302 ms) [ ] http://reallylongurl 0xd68d6603 NORMAL 2
11-15 12:29:48.412: D/Volley(14666): [1] MarkerLog.finish: (+0 ) [ 1] add-to-queue
11-15 12:29:48.412: D/Volley(14666): [1] MarkerLog.finish: (+15164) [188] cache-queue-take
11-15 12:29:48.412: D/Volley(14666): [1] MarkerLog.finish: (+220 ) [188] cache-hit
11-15 12:29:48.432: D/Volley(14666): [1] MarkerLog.finish: (+2299) [188] cache-hit-parsed
11-15 12:29:48.432: D/Volley(14666): [1] MarkerLog.finish: (+0 ) [188] post-response
11-15 12:29:48.442: D/Volley(14666): [1] MarkerLog.finish: (+619 ) [ 1] done
As you can see it never says another request was added nor does it say more then one request was in flight. If I clear out the cache I get the same result, just the first request is from network and the second is returned from cache. I have tried debugging and stepping through my code but I never see the request being enqueued more then once. Has anyone seen this before? Any where else I should look?
Thanks
Edit: Here is the code where I start up volley and where I call it.
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState == null) {
TextView emptyView = new TextView(getActivity());
emptyView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
emptyView.setText("Loading....");
emptyView.setTextSize(20);
emptyView.setGravity(Gravity.CENTER_VERTICAL
| Gravity.CENTER_HORIZONTAL);
((ViewGroup) mListView.getParent()).addView(emptyView);
mListView.setEmptyView(emptyView);
}
mAdapter = new OttoArrayAdapter(mContext);
mListView.setOnScrollListener(onScrollListener);
mListView.setAdapter(mAdapter);
String url = Util.getURL("", mContext);
HttpRequest request = new HttpRequest(url);
request.setTag(mContext);
VolleyLoader.getInstance(mContext).getRequestQueue().add(request);
}
public class VolleyLoader {
private static VolleyLoader mInstance = null;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private VolleyLoader(Context context) {
OkHttpStack stack = new OkHttpStack();
mRequestQueue = Volley.newRequestQueue(context, stack);
mImageLoader = new ImageLoader(this.mRequestQueue, new LruBitmapCache(
Util.getCacheSize(context)));
}
public static VolleyLoader getInstance(Context context) {
if (mInstance == null) {
mInstance = new VolleyLoader(context);
}
return mInstance;
}
public RequestQueue getRequestQueue() {
return this.mRequestQueue;
}
public ImageLoader getImageLoader() {
return this.mImageLoader;
}
}
volley (lib) is not deprecated. Why would it be? More importantly, if it works, why does it matter? Deprecated doesn't necessarily mean something is bad.
1 Answer. Show activity on this post. To achieve it without using any patterns or other libraries, you can mark the request as finished if it responded, and call the method, in each of them, you want to execute if all the requests are finished. On that method, you just need to check if all the requests are done.
Your singleton class contains a static reference to the context which can cause a memory leak. Instead of this, you can use an application instance like this public class AppController extends Application { public static final String TAG = AppController. class.
By default, Volley sets all socket and connection timeouts to 5 seconds for all requests. RetryPolicy is an interface where you need to implement your logic of how you want to retry a particular request when a timeout occurs.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With