Volley: How to set up a Cache.Entry

android, android-volley

Solution

I've had the same issue and ended up with this solution:

@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {

    // Create a FakeCache that invalidate the data after 24 hour
    Cache.Entry mFakeCache = HttpHeaderParser.parseCacheHeaders(response);
    mFakeCache.etag = null;
    mFakeCache.softTtl = System.currentTimeMillis() + 86400 * 1000;
    mFakeCache.ttl = mFakeCache.softTtl;

    return Response.success(response.data, mFakeCache);
}

Problem

I am using Google's Volley Library as my design for getting network data; I have set up a RequestQueue ``` requestQueue = new RequestQueue(new DiskBasedCache(new File(context.getCacheDir(), DEFAULT_CACHE_DIR)), new BasicNetwork(new HttpClientStack(AndroidHttpClient.newInstance(userAgent)))); ``` I have also subclassed Request, and have data coming back from the network just fine. My issue is with caching: in parseNetworkResponse() which is overridden in my subclass of Request, when I call ``` return Response.success(list, HttpHeaderParser.parseCacheHeaders(response)); ``` HttpHeaderParser.parseCacheHeaders(response) returns null since the server is set up for "no caching" in its response header... Regardless I still would like to cache this data for a variable set number of hours (24 hours probably), How can I do this by creating a volley Cache.Entry... It is my understanding that the URL is used as the cache key value (and I would like it to be the URL). To sum up, since HttpHeaderParser.parseCacheHeaders(response) returns null, I would like to create a new Cache.Entry that is set up for expiring after 24 hours, and the cache key being the URL of the request. Any thoughts? Thanks!

Original source