2015-08-28 37 views
1

我想给我的应用程序提供离线支持,并且我使用排球库来解析来自jsonobject的数据。我了解到凌空本身有一个很好的缓存机制。我已经实现此代码以从缓存中获取数据排球缓存总是在android中返回null

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

但该条目始终返回空值。我听说过的服务器站点的标题,我已经使用Mozilla的RESTClient实现addonn看起来像这样测试它:

header from the server

我不知道问题出在哪里,但我总是得到缓存条目空,如果我像凌乱一样对凌乱的网络进行分析。我现在应该怎么做?

+1

请阅读更多在[Android设定排球从缓存使用(http://stackoverflow.com/questions/31897189/android-setup-volley-to-use-from-cache/32022946#32022946) – BNK

+0

我会的,但你能解释这里的错误吗? – Reyjohn

+0

也许你的服务器应用程序不允许缓存。你可以试试'Cache.Entry cacheEntry = HttpHeaderParser.parseCacheHeaders(response);'parseNetworkResponse'内部 – BNK

回答

1

Volley不必只使用url作为缓存键。

请致电Request.getCacheKey()获取缓存密钥。

/** 
* 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.