需要Android中的HttpResponseCache示例

需要Android中的HttpResponseCache示例,第1张

概述嗨,我正在尝试使用 Android 4中引入的HttpResponseCache.该文档清楚地说明如何安装缓存,但是我完全失去了如何缓存从网络下载的图像.更早我正在使用DiskLruCache缓存它们.任何人都会指出一些使用HttpResponseCache的工作代码的例子. 编辑: – 有人可以告诉我我在这里做错了什么: MainActivity.javapublic void onCreat 嗨,我正在尝试使用 Android 4中引入的httpresponsecache.该文档清楚地说明如何安装缓存,但是我完全失去了如何缓存从网络下载的图像.更早我正在使用diskLruCache缓存它们.任何人都会指出一些使用httpresponsecache的工作代码的例子.

编辑: – 有人可以告诉我我在这里做错了什么:

MainActivity.javapublic 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 staleconnection.addRequestProperty("Cache-Control","max-stale=" + maxStale);
总结

以上是内存溢出为你收集整理的需要Android中的HttpResponseCache示例全部内容,希望文章能够帮你解决需要Android中的HttpResponseCache示例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1134131.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存