-->

需要在Android的一个HttpResponseCache的例子(Need an example

2019-06-26 10:30发布

您好我想使用Android中4.文档介绍了HttpResponseCache有关如何安装缓存也要讲清楚,但我就如何我用的是DiskLruCache对其进行缓存缓存来自net.Earlier下载的图片完全丧失。 会有人点我驶向何方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。

请让我知道,如果有任何其他的信息,你要我提供

Answer 1:

从局部一个缓存响应 HttpResponseCache文档 :

有时候你会想显示资源,如果他们立即可用,而不是其他。 这可以用来使你的应用程序可以显示的东西在等待最新的数据被下载。 要限制对本地高速缓存的资源的请求,加上only-if-cached指令:

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
}

这种技术更好的在一个陈旧的响应比无反应更好的情况。 要允许过期缓存的响应,使用max-stale指令以秒的最大过时:

int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale
connection.addRequestProperty("Cache-Control", "max-stale=" + maxStale);


Answer 2:

当您启用HttpResponseCache ,所有HttpUrlConnection查询将被缓存。 你不能用它来缓存任意数据,所以我建议你继续使用DiskLruCache了点。



Answer 3:

在我的情况HttpResponseCache实际上没有任何缓存。 什么固定它很干脆:

connection.setUseCaches(true);

(这必须在调用HttpURLConnection建立连接之前。)

对于更细粒度的控制, max-stale可以作为杰西·威尔逊指出 。



文章来源: Need an example of HttpResponseCache in Android