- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用 HttpResponseCache
我的场景如下:
我在以下 GitHub API URL 上对其进行了测试: https://api.github.com/users/blackberry/repos
我的代码如下
String loadData() {
HttpURLConnection urlConnection = null;
String response = null;
try {
URL url = new URL(
"https://api.github.com/users/blackberry/repos");
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(
urlConnection.getInputStream());
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
StringBuffer buffer = new StringBuffer();
String s = null;
while ((s = reader.readLine()) != null) {
buffer.append(s);
}
response = buffer.toString();
} catch (MalformedURLException e) {
Log.e("MalformedURLException", e.toString());
} catch (IOException e) {
Log.e("IOException", e.toString());
} finally {
urlConnection.disconnect();
HttpResponseCache cache = HttpResponseCache.getInstalled();
if (cache != null) {
cache.flush();
}
}
return response;
}
当我测试离线模式时,在以下位置抛出 IO 异常:
InputStream in = new BufferedInputStream(
urlConnection.getInputStream());
这里有什么问题吗?
最佳答案
这为时已晚,但可能会对某人有所帮助。我遇到了同样的问题,因为当网络可用时,它不会强制从服务器获取数据并获取缓存数据。
if (IsNetAvailable(context)) {
urlConnection.addRequestProperty("Cache-Control", "max-age=0");
// urlConnection.setUseCaches(true);
} else {
int maxStale = 60 * 10; // tolerate 10 mins
urlConnection.addRequestProperty("Cache-Control", "max-stale=" + maxStale);
urlConnection.setUseCaches(true);
}
我使用了相同的代码并且可以很好地满足以下要求。
<强>1。网络可用时强制从服务器获取数据。
<强>2。离线时从缓存中获取数据。
关于Android:HttpResponseCache:在没有互联网连接时检索缓存的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23109270/
我关注了 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
我是一名优秀的程序员,十分优秀!