我想在我的应用程序中提供离线支持,我使用 volley 库来解析来自 jsonobject
的数据。我了解到 volley 本身有一个很好的缓存机制。我已经实现了这段代码来从缓存中获取数据
Cache cache = GlobalData.getInstance().getRequestQueue().getCache();
Cache.Entry entry = cache.get(Constants.URL_WEATHER_REQUEST + city);
但该条目始终返回 null。我听说过服务器站点的 header ,我已经使用 mozilla 中的 restClient addonn 对其进行了测试,如下所示:
我不知道问题出在哪里,但我总是得到空缓存条目,如果我打开互联网,截击会像往常一样解析它。我现在该怎么办?
最佳答案
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;
}
关于android - Volley 缓存总是在 android 中返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32266705/