1

I am using Volley for request and I need cache my json values. I read this link I did it it worked but there is one issue.

I have a listView which item is item1 and item2. When I click in item1 it cache json and when I click item2 it returns from cache. The result is it shows only item1 values. What should I change here so caching will be correct.

public static Cache.Entry parseIgnoreCacheHeaders(NetworkResponse response) {
        long now = System.currentTimeMillis();

        Map<String, String> headers = response.headers;
        long serverDate = 0;
        String serverEtag = null;
        String headerValue;

        headerValue = headers.get("Date");
        if (headerValue != null) {
            serverDate = parseDateAsEpoch(headerValue);
        }

        serverEtag = headers.get("ETag");

        final long cacheHitButRefreshed = 3 * 60 * 1000; // in 3 minutes cache will be hit, but also refreshed on background
        final long cacheExpired = 24 * 60 * 60 * 1000; // in 24 hours this cache entry expires completely
        final long softExpire = now + cacheHitButRefreshed;
        final long ttl = now + cacheExpired;

        Cache.Entry entry = new Cache.Entry();
        entry.data = response.data;
        entry.etag = serverEtag;
        entry.softTtl = softExpire;
        entry.ttl = ttl;
        entry.serverDate = serverDate;
        entry.responseHeaders = headers;

        return entry;
    }
Community
  • 1
  • 1
pmb
  • 2,327
  • 3
  • 30
  • 47

1 Answers1

0

For caching is great to use VolleyExtended: eu.the4thfloor.volleyextended There is updated Response listener, where you get additional boolean changed which means if response is different to last response in cache. And on error response there is additional parameter response (cached) which you can use in case of server or network outtage. Of course if it is cached only. Of course caching is there dependent on the max-age parameter sent from server. But you can update this in volley as you wish

private class ResponseListenerYYY extends Api.ResponseListener {
@Override
public void onResponse(String response, boolean changed) {

}

@Override
public void onErrorResponse(VolleyError error, String response) {


}

}

maxxxo
  • 672
  • 3
  • 10
  • 28