- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
您好,我正在尝试使用 Android 4 中引入的 HttpResponseCache。文档确实清楚地讨论了如何安装缓存,但我完全不知道如何缓存从网络下载的图像。之前我使用的是 DiskLruCache缓存它们。谁能给我指出一些使用了 HttpResponseCache 的工作代码示例。
编辑:- 谁能告诉我我做错了什么:-
MainActivity.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
final File httpCacheDir = new File(getCacheDir(), "http");
try {
Class.forName("android.net.http.HttpResponseCache")
.getMethod("install", File.class, long.class)
.invoke(null, httpCacheDir, httpCacheSize);
Log.v(TAG,"cache set up");
} catch (Exception httpResponseCacheNotAvailable) {
Log.v(TAG, "android.net.http.HttpResponseCache not available, probably because we're running on a pre-ICS version of Android. Using com.integralblue.httpresponsecache.HttpHttpResponseCache.");
try{
com.integralblue.httpresponsecache.HttpResponseCache.install(httpCacheDir, httpCacheSize);
}catch(Exception e){
Log.v(TAG, "Failed to set up com.integralblue.httpresponsecache.HttpResponseCache");
}
}
TheMainListFrag gf=(TheMainListFrag) getSupportFragmentManager().findFragmentByTag("thelistfrags");
if(gf==null){
gf=TheMainListFrag.newInstance();
FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
ft.replace(R.id.thelefty, gf,"thelistfrags");
ft.commit();
}
}
然后在 TheMainListFrag 的加载器中,我执行以下操作:-
public ArrayList<HashMap<String,String>> loadInBackground() {
String datafromServer = null;
ArrayList<HashMap<String,String>> al = new ArrayList<HashMap<String,String>>();
try {
String url = "someurl";
HttpURLConnection urlConnection = (HttpURLConnection) new URL(url).openConnection();
urlConnection.setRequestProperty("Accept", "application/json");
InputStream is = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line);
}
datafromServer=sb.toString();
Log.v("fromthread",datafromServer);
// etc
//etc
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
Log.v("fromthread", e.getClass() + "--" + e.getMessage());
}
return al;
}
当我连接到互联网时,它工作正常,并且在目录 http - 上面命名的缓存目录中,我也可以看到文件。但是当我没有连接到互联网时,数据拒绝加载。
当我从网络加载图像时,我看到名为 .tmp 的缓存文件,我认为根据 DiskLruCache 将其称为脏文件。
如果您希望我提供任何其他信息,请告诉我
最佳答案
来自 HttpResponseCache documentation 上的强制缓存响应部分:
Sometimes you'll want to show resources if they are available immediately, but not otherwise. This can be used so your application can show something while waiting for the latest data to be downloaded. To restrict a request to locally-cached resources, add the
only-if-cached
directive:
try {
connection.addRequestProperty("Cache-Control", "only-if-cached");
InputStream cached = connection.getInputStream();
// the resource was cached! show it
} catch (FileNotFoundException e) {
// the resource was not cached
}
This technique works even better in situations where a stale response is better than no response. To permit stale cached responses, use the
max-stale
directive with the maximum staleness in seconds:
int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale
connection.addRequestProperty("Cache-Control", "max-stale=" + maxStale);
关于android - 需要一个 Android 中的 HttpResponseCache 的例子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11584761/
我关注了 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
我是一名优秀的程序员,十分优秀!