android - Volley 缓存总是在 android 中返回 null

标签 android caching android-volley

我想在我的应用程序中提供离线支持,我使用 volley 库来解析来自 jsonobject 的数据。我了解到 volley 本身有一个很好的缓存机制。我已经实现了这段代码来从缓存中获取数据

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

但该条目始终返回 null。我听说过服务器站点的 header ,我已经使用 mozilla 中的 restClient addonn 对其进行了测试,如下所示:

header from the server

我不知道问题出在哪里,但我总是得到空缓存条目,如果我打开互联网,截击会像往常一样解析它。我现在该怎么办?

最佳答案

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.

关于android - Volley 缓存总是在 android 中返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32266705/

相关文章:

jQuery 缓存选择器

c# - 如何检查 System.Runtime.Caching.ObjectCache 中的缓存策略?

caching - 将事实收集到事实缓存的最快方法

android - 如何使用 Android Studio 和 Web Api 对同一请求进行 POST 和 GET

android - Volley 返回403错误

javascript - 在 WebView 中打开一个弹出窗口

java - Android 还剩多少天

android - 如果移动3G每30分钟检查一次互联网,为什么推送消息会立即到来?

android - 操作栏 Sherlock NAVIGATION_MODE_LIST - dropdownListPreferredItemHeight 被忽略

android - 如何使用 Volley 解析 JSON