2

I am trying to get Volley to work using its Cache. When I receive a 304 getCacheEntry().data is null even though "cache-control" is set. Here is what I am doing:

  1. Volley gets instantiated likes this

    // Instantiate the cache
    Cache cache = new DiskBasedCache(c.getCacheDir(), 10 * 1024 * 1024); // 10 MB cap
    
    // Set up the network to use HttpURLConnection as the HTTP client.
    Network network = new BasicNetwork(new HurlStack());
    
    // Instantiate the RequestQueue with the cache and network.
    mRequestQueue = new RequestQueue(cache, network);
    
    // Start the queue
    mRequestQueue.start();
    
  2. After sending a GET request I get the response 200 with "cache-control" set to "max-age=180, public". So far so good right?

  3. If a GET request gets made twice or more I set "If-Modified-Since" with the last timestamp the request was made to the request header.
  4. The second time I request a specific API endpoint the sever will respond with a 304. getCacheEntry().data returns null though. If I check the cache entries in Volleys RequestQueue I cannot find an entry for my specific request.

What am I doing wrong? For some reason I have one request that is always cached when fired once. It's even one that returns a lot of data. But all other requests aren't cached. Following code snippet parses the response and checks for 304.

@Override
protected Response<T> parseNetworkResponse(NetworkResponse response, String charset) { 
    try {
        String json = "";
        if (!response.notModified) {
            json = new String(response.data, charset);
        } else {
            //if not modified -> strangely getCacheEntry().data always null
            json = new String(getCacheEntry().data, charset);
        }
        return Response.success(
                gson.fromJson(json, clazz),
                HttpHeaderParser.parseCacheHeaders(response));

    } catch (UnsupportedEncodingException e) {
        return Response.error(new ParseError(e));
    } catch (JsonSyntaxException e) {
        return Response.error(new ParseError(e));
    }
}

I really appreciate any comments on this.

Ben
  • 751
  • 7
  • 9
  • If your request is a POST request, IMO you can read the following http://stackoverflow.com/questions/21953519/volley-exception-error-when-response-code-304-and-200 – BNK Nov 09 '15 at 10:21
  • Thanks @BNK it is a GET request though – Ben Nov 09 '15 at 13:50
  • If the server is published in Internet, pls post the url so that I can check tomorrow – BNK Nov 09 '15 at 14:09
  • 1
    IMO, you can set breakpoint at `Cache.Entry entry = mCache.get(request.getCacheKey());` inside `run` of `CacheDispatcher` first to see if it null or not, this line will run before you got 304 from server – BNK Nov 10 '15 at 01:57

1 Answers1

0

IMHO, your cache entry is always null (not only when getting 304 resp code), because of the following:

Cache cache = new DiskBasedCache(c.getCacheDir(), 10 * 1024 * 1024); // 10 MB cap

Please check your c.getCacheDir() to see if you want to use External Storage to store cache data, then you should set WRITE_EXTERNAL_STORAGE permission inside AndroidManifest.xml file.

Hope this helps!

BNK
  • 23,994
  • 8
  • 77
  • 87
  • I solved this issue! `WRITE_EXTERNAL_STORAGE` is set in `AndroidManifest.xml`. But in some old legacy class we generated a new `RequestQueue` for some strange reason ... now it's working. Thank you for your help!! – Ben Nov 10 '15 at 10:17