Android Volley - HttpResponseCache

时间:2015-02-22 10:02:22

标签: android caching httpurlconnection android-volley

我正在构建一个Android应用程序,它使用Volley从网络加载图像。

我正在使用Volley的LruCache来缓存内存中的位图,这很好。另外,我希望Volley能够利用内置的基于http盘的缓存支持HttpResponseCache

我在给定的链接中实现了示例,但是我注意到HttpResponseCache中没有任何内容被缓存(我通过对缓存对象中的HitCount字段进行抽样检查)。

在Volley源代码中嗅探后,我发现Volley在打开URLConnection时手动删除了缓存标记:

private URLConnection openConnection(URL url, Request<?> request) throws IOException {
        URLConnection connection = createConnection(url);

        int timeoutMs = request.getTimeoutMs();
        connection.setConnectTimeout(timeoutMs);
        connection.setReadTimeout(timeoutMs);
        connection.setUseCaches(false); // <-- Disables the caching
        connection.setDoInput(true);

        // use caller-provided custom SslSocketFactory, if any, for HTTPS
        if ("https".equals(url.getProtocol()) && mSslSocketFactory != null) {
            ((HttpsURLConnection)connection).setSSLSocketFactory(mSslSocketFactory);
        }

        return connection;
    }

您可以在此处查看此代码:Volley - HurlStack(第167行)

当我注释掉这一行时,HttpResponseCahce按预期工作。

我的问题是:为什么 Volley会在所有URLConnections中禁用UseCaches标志,以及删除此问题的风险/副作用是什么?

0 个答案:

没有答案
相关问题