- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试使用 httpResponseCache缓存我的一些请求。问题是,我想知道在发出请求之前是否缓存了请求。
我在源代码中看到了 HttpResponseCache有一个 get 方法接受 (URI, requestMethod, Map< String, List< String >>) 并返回一个 cachedResponse。我尝试了几组不同的(URI、“GET”、标题),但我似乎无法弄清楚标题是什么,每次我使用 get 请求 cachedResponse 时,我都会返回 null,甚至尽管缓存了一个响应。
有没有人成功地使用 get 方法来确定在发出请求之前是否缓存了请求/或者是否有不同的方法来检查在发出请求之前是否缓存了某些内容?我知道我可以使用 hitCount 来确定响应是否来自缓存,并且我可以使用“Cache-Control”、“only-if-cached”来强制缓存响应。我只想检查它是否被缓存。
这是我用来尝试完成此操作的一些代码。在 Activity 的 onCreate 中,我确实以这种方式初始化缓存:
try {
File httpCacheDir = new File(this.getCacheDir(), "http");
long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
Class.forName("android.net.http.HttpResponseCache")
.getMethod("install", File.class, long.class)
.invoke(null, httpCacheDir, httpCacheSize);
}
catch (Exception httpResponseCacheNotAvailable) {
Log.d(TAG,"HttpResponseCache not available.");
}
然后尝试这样使用它:
HttpResponseCache cache = HttpResponseCache.getInstalled();
try {
HashMap map = new HashMap<String, List<String>>();
CacheResponse response = cache.get(URI.create(base_url), "GET", map);
//response is always null
Log.d(TAG," Response is!:"+response);
} catch (IOException e) {
e.printStackTrace();
}
抱歉/谢谢/非常感谢任何帮助!
最佳答案
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
HttpResponseCache cache = HttpResponseCache.getInstalled();
cache.get(new URI(url.toString()), "GET", connection.getHeaderFields());
应该是这样的,不过我还没试过。
关于android - 使用 HttpResponseCache.get 时要使用的 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26241575/
我关注了 developers guide并且我通过在应用程序的 onCreate() 方法中包含以下代码来安装 HttpResponseCache: try { File httpCacheDi
我很快将在一个项目上工作,该项目使用大量 HTTPRequests 主要用于 JSON 和 Images,所以我认为它是考虑缓存是个好主意。基本上我正在寻找 的解决方案 启动具有给定生命周期(例如 3
我正在尝试使用 HttpResponseCache,但没能成功。 我是这样安装的: SdCardManager manager = SdCardManager.getSdCardMan
我想使用 HttpResponseCache 我的场景如下: 当互联网连接可用时,服务器上的数据会正常检索并保存到缓存中。 当没有互联网连接时,将检索并显示缓存数据。 我在以下 GitHub API
我正在尝试缓存此文件:http://media.admob.com/sdk-core-v40.js与 HttpResponseCache。基本上我调用了 getFileFromUrl 函数两次,以便第
我在 Android 中缓存来自 Web 服务器的 http(s) 响应时遇到问题,文档很差,所以我在这里寻求帮助。这是我的代码: String encodedString = String.
我正在尝试使用 HttpResponseCache 来缓存网络数据,但是当我将安装缓存代码放入 OnCreate 和 OnPause 方法时,应用崩溃。我使用了 HttpResponseCache d
我正在使用 gridview 显示数百张图像(甚至可能是几千张)。图片位于服务器上,我正在使用 HttpResponseCache 缓存图片.我遇到的问题是,当我向下滑动 gridview 时,回收的
我正在开发一个与 API 14( Ice Cream Sandwich )兼容的 Android 应用程序。我找到了可用于缓存 HTTP 请求的类 HttpResponseCache。我想将缓存与我的
我正在使用 HttpResponseCache在我的 Android 应用程序中启用响应缓存(用于 Web 请求),并且离线缓存不工作。我正在做离线缓存作为 documentation告诉我去做。 在
我需要缓存 http 响应,并基于此 blog post该功能内置于 ICS 中。但是,我需要支持 1.6 版及更高版本,因此想在查看向后移植 ICS 功能需要什么之前先看看是否有可用的库。 更新:
我正在尝试在导入的Android Studio中编译一个Android项目: com.squareup.okhttp.HttpResponseCache 我收到错误消息“无法解析符号'HttpResp
我一直在我的应用程序中成功使用 HttpResponseCache,但是当我的手机更新到 Lollipop 时,我意识到 HttpResponseCache 现在永远不会“命中”,总是执行网络请求。我
您好,我正在尝试使用 Android 4 中引入的 HttpResponseCache。文档确实清楚地讨论了如何安装缓存,但我完全不知道如何缓存从网络下载的图像。之前我使用的是 DiskLruCach
我正在尝试使用 httpResponseCache缓存我的一些请求。问题是,我想知道在发出请求之前是否缓存了请求。 我在源代码中看到了 HttpResponseCache有一个 get 方法接受 (U
最初的目标是同时使用磁盘和内存缓存。这需要实现 LRU 缓存和 DiskLruCache。但是,由于 HTTPResponse 缓存使用磁盘空间,我选择使用 LRU 缓存并执行 con.setUseC
我正在尝试在一个新的 Android 项目中测试这个缓存库。 当我运行项目时,我得到这个 Logcat: 01-24 03:45:31.109: E/AndroidRuntime(1983): FAT
我有以下方法从 wiktionary.org 请求页面,问题是服务器在 header 中返回 Cache-control => private, must-revalidate, max-age=0
我正在开发一个基于 WebView 的应用程序,该应用程序当前在 v3.1 平板电脑上运行。我似乎无法让 WebView 缓存 css、js 和图像(或使用缓存)。该应用似乎总是连接到服务器,服务器返
我正在尝试在我的应用程序中使用内置的 HTTPResponseCache(通过 HTTPURLConnection API 发出请求)但是在尝试让它缓存任何响应时遇到了问题请求时包含一个 Author
我是一名优秀的程序员,十分优秀!