【问题标题】:Volley cache always returns null in androidVolley 缓存总是在 android 中返回 null
【发布时间】:2015-08-28 08:35:02
【问题描述】:

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

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

但该条目始终返回 null。我听说过服务器站点的标头,并在 mozilla 中使用 restClient 插件对其进行了测试,如下所示:

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

【问题讨论】:

  • 请在Android Setup Volley to use from Cache阅读更多内容
  • 我会的,但你能解释一下这里哪里错了吗?
  • 可能您的服务器应用不允许缓存。你可以在parseNetworkResponse里面试试Cache.Entry cacheEntry = HttpHeaderParser.parseCacheHeaders(response);
  • 您的问题解决了吗?
  • 嗨@Reyjohn 你找到解决这个问题的方法了吗?我也面临同样的情况。在我以前的项目中它运行良好,我不知道现在发生了什么。

标签: android caching android-volley


【解决方案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.

【讨论】:

    猜你喜欢
    • 2021-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-31
    • 2018-01-16
    • 2012-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多