Android HttpResponseCache 不工作 - FileNotFoundException

标签 android http caching response httpresponsecache

我在 Android 中缓存来自 Web 服务器的 http(s) 响应时遇到问题,文档很差,所以我在这里寻求帮助。这是我的代码:

    String encodedString = String.format("jsonData=%s", URLEncoder.encode(json, "UTF-8"));

    URL urlConn = new URL(url+"?"+encodedString);

    HttpURLConnection cachedUrlConnection = (HttpURLConnection) urlConn.openConnection();
    cachedUrlConnection.setUseCaches(true);
    cachedUrlConnection.setDoInput(true);
    cachedUrlConnection.setDoOutput(true);
    cachedUrlConnection.setRequestProperty("charset", "utf-8");
    cachedUrlConnection.setRequestMethod("GET");

    cachedUrlConnection.addRequestProperty("Cache-Control", "only-if-cached");


    InputStream is = null;
    try {
        is = cachedUrlConnection.getInputStream();

        Log.i("_INFO","------CACHE FOUNDED");

    } catch (FileNotFoundException e){
        e.printStackTrace();

        HttpURLConnection nonCachedUrlConnection = (HttpURLConnection) urlConn.openConnection();
        nonCachedUrlConnection.setUseCaches(true);
        nonCachedUrlConnection.setDoInput(true);
        nonCachedUrlConnection.setDoOutput(true);
        nonCachedUrlConnection.setRequestProperty("charset", "utf-8");
        nonCachedUrlConnection.setRequestMethod("GET");
        nonCachedUrlConnection.addRequestProperty("Cache-Control", "max-stale=" + (60 * 60 * 24));

        Log.i("_INFO","-------CACHE NOT FOUNDED");

        is = nonCachedUrlConnection.getInputStream();
    }

此外,我已经在 Application onCreate 方法上安装了缓存,如下所示:

try {
        long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
        File httpCacheDir = new File(getExternalCacheDir(), "http");
        Class.forName("android.net.http.HttpResponseCache")
                .getMethod("install", File.class, long.class)
                .invoke(null, httpCacheDir, httpCacheSize);

        Log.i("_INFO", "HttpResponseCache enabled");

    } catch (Exception httpResponseCacheNotAvailable) {
        Log.d("_INFO", "HTTP response cache is unavailable.");
    }

如果我打印缓存大小,在应用程序重新启动后,它会正确打印 2 mb 的缓存大小(因此它可以正确缓存)。我在所有 HTTP 调用后刷新缓存,如下所示:

HttpResponseCache cache = HttpResponseCache.getInstalled();
        if(cache != null) {
            Log.i("_INFO", "CACHED FLUSHED WITH " + cache.size());
            cache.flush();
        }

所以,基本上,缓存过程运行良好,但是当我获取 InputStream 时我无法获取缓存的响应。我的 logcat 总是打印“CACHE NOT FOUNDED”。

最佳答案

一段时间后,我找到了解决方案,似乎我的 HTTP 调用中的参数太多,或者可能是一个错误的参数。

只有:

cachedUrlConnection.addRequestProperty("Cache-Control", "max-stale=" + maxStale);

这一个,缓存工作很迷人。

关于Android HttpResponseCache 不工作 - FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26481434/

相关文章:

python - 从 `urllib2.urlopen` 获取错误文本

android - 基于 BLE 的 Android 应用的密封测试

android - 时间选择器 : when to choose 24 or am/pm modes

node.js - 为什么 Web 服务器仍然使用 http 1.1 而不是 http 2?

java - 在 HTTPServlet doPost() 中将 maxage(0) 设置为 cookie 并且重定向不起作用

java - 什么是类似于哈希表的数据结构,但是不常用的键被删除了?

android 上的 javascript 地理定位不起作用

java - 使用 ObjectInputStream 从 Assets 中读取二进制文件

symfony - 教义二级缓存不执行任何操作

python - 在Python中实现缓存