1

I want to give offline support in my app and I used volley library to parse data from jsonobject. I have learned that volley itself has a fine cache mechanism. I have implemented this code to get data from cache

Cache cache = GlobalData.getInstance().getRequestQueue().getCache();
Cache.Entry entry = cache.get(Constants.URL_WEATHER_REQUEST + city);

but the entry always returns null. I have heard about the headers of the server site and I have tested it using restClient addonn in mozilla which looks like this:

header from the server

I dont know where is the problem but I always get cache entry null and If I turn the internet on volley parses it as usual. What should I do now?

Reyjohn
  • 2,654
  • 9
  • 37
  • 63
  • 1
    Please read more at [Android Setup Volley to use from Cache](http://stackoverflow.com/questions/31897189/android-setup-volley-to-use-from-cache/32022946#32022946) – BNK Aug 28 '15 at 08:38
  • I will, but can you explain where is the wrong here? – Reyjohn Aug 28 '15 at 08:39
  • Perhaps your server app does not allow caching. You can try `Cache.Entry cacheEntry = HttpHeaderParser.parseCacheHeaders(response);` inside `parseNetworkResponse` – BNK Aug 28 '15 at 08:50
  • Have your issue been solved yet? – BNK Aug 28 '15 at 10:00
  • Hi @Reyjohn did you find any solution to this problem? I'm facing same. In my previous projects it was working fine, i dont what happened now. – Nauman Zubair Oct 15 '15 at 14:54
  • @NaumanZubair: if your server app supports output caching, `Volley` will automatically cache the response, if your server app does not support, you can refer to my above link. – BNK Oct 29 '15 at 04:50
  • Thanks for your comment, its figured out already. :) – Nauman Zubair Oct 29 '15 at 07:55

1 Answers1

1

Volley doesn't have to use only the url as a cache key.

Call Request.getCacheKey() to get the cache key.

/**
 * Returns the cache key for this request.  By default, this is the URL.
 */
public String getCacheKey() {
    return mMethod + ":" + mUrl;
}

/**
 * Supported request methods.
 */
public interface Method {
    int DEPRECATED_GET_OR_POST = -1;
    int GET = 0;
    int POST = 1;
    int PUT = 2;
    int DELETE = 3;
}

The issue on Github.

headsvk
  • 2,726
  • 1
  • 19
  • 23