dumazy
dumazy

Reputation: 14445

Check if Volley gets results from cache or over network

How can I check whether Volley gets the results of a JsonObjectRequest from the cache or from the network?

I need to show a progress dialog when it needs a network connection but not when the results are quickly received from the cache.

my request looks something like this

volleyQueue = Volley.newRequestQueue(this);
JsonObjectRequest jr = new JsonObjectRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>(){...stuff}, new Response.ErrorListener(){...errorstuff});
jr.setShouldCache(true);
volleyQueue.add(jr);

Upvotes: 16

Views: 8224

Answers (5)

user3703478
user3703478

Reputation: 21

adb shell setprop log.tag.Volley VERBOSE

Run this command in your terminal, you may need to set 'adb' in your path in order to use that command, it should be located in your sdk/platform-tools/ dir.

This will provide much more detailed volley logs and will show something along the lines of an execution stack for a volley request which exhibits cache hits or misses.

Upvotes: 1

a fair player
a fair player

Reputation: 11776

Starting from Tim Kelly's answer.

by the time you check "cacheHit", it'll be reverted to false and you'll not know that it's a cache hit because many other tags are received after "cacheHit" is received and before the "onResponse" is called.

So, add

if(tag.equals("network-http-complete")){
            cacheHit = false;
        }

and remove cacheHit = false;

Upvotes: 2

Emanuel Canha
Emanuel Canha

Reputation: 441

Before making the Request you can get the cache from the Request Queue and check if the Entry is not null.

mRequestQueue.getCache().get("key");

The key for each request is usually the URL. I guess you should have to check if the Entry has expired too.

Upvotes: 4

tkelly
tkelly

Reputation: 642

I did this by overriding Request#addMarker and checking for a "cache-hit" marker being added:

public class MyRequest<T> extends Request<T> {

    protected boolean cacheHit;

    @Override
    public void addMarker(String tag) {
        super.addMarker(tag);
        cacheHit = false;
        if (tag.equals("cache-hit")){
            cacheHit = true;
        }
    }
}

Upvotes: 11

Itai Hanski
Itai Hanski

Reputation: 8690

Volley has a built in way to know if image requests are immediate through the ImageContainer class, but it doesn't seem to have a similar mechanism for other requests such a JSON object request.

It seems that you have 2 main choices:

  1. You can set a timer for something like 300ms after you request the JSON (test for the best time). When the timer is done, check to see if you have the result already, otherwise show the dialog. I know this is a bit of a "hack" but it could be good enough.
  2. Edit the Volley code to add an "isImmediate" flag to every request. There are multiple ways to achieve this. I suggest starting at CacheDispatcher

Upvotes: 2

Related Questions