HttpResponseCache类的测试:
具体程序工程如下:
package org.durian.durianhttpresponsecache; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.http.HttpResponseCache; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class DurianMainActivity extends ActionBarActivity implements View.OnClickListener { private final static String TAG="DurianMainActivity"; private Button mButton; private Button mReButton; private ImageView imageView; private HttpHandler mHttpHandler; private Bitmap bitmap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.durian_main); File httpCacheDir=new File(DurianMainActivity.this.getCacheDir(),"http"); long httpCacheSize=10*1024*1024; try { HttpResponseCache.install(httpCacheDir,httpCacheSize); } catch (IOException e) { e.printStackTrace(); } mButton=(Button)findViewById(R.id.button); mButton.setOnClickListener(this); mReButton=(Button)findViewById(R.id.reload); mReButton.setOnClickListener(this); imageView=(ImageView)findViewById(R.id.image); mHttpHandler=new HttpHandler(DurianMainActivity.this); bitmap=BitmapFactory.decodeResource(this.getResources(),R.mipmap.ic_launcher); } @Override protected void onStop() { super.onStop(); HttpResponseCache cache=HttpResponseCache.getInstalled(); if(cache!=null){ cache.flush(); } } private downInformationFromNet work=new downInformationFromNet(); @Override public void onClick(View v) { int id=v.getId(); switch (id){ case R.id.button: work.execute(); break; case R.id.reload: bitmap=BitmapFactory.decodeResource(this.getResources(),R.mipmap.ic_launcher); imageView.setImageBitmap(bitmap); break; default: break; } } private class HttpHandler extends Handler{ private DurianMainActivity durian; public HttpHandler(Context context){ durian=(DurianMainActivity)context; } @Override public void handleMessage(Message msg) { super.handleMessage(msg); durian.imageView.setImageBitmap(bitmap); } } private class downInformationFromNet extends AsyncTask<Integer,String,String>{ @Override protected String doInBackground(Integer... params) { try { URL url=new URL("http://pic31.nipic.com/20130724/5252423_104848296000_2.jpg"); try { HttpURLConnection connection=(HttpURLConnection)url.openConnection(); connection.connect(); if(connection.getResponseCode()==HttpURLConnection.HTTP_OK){ InputStream is=connection.getInputStream(); bitmap= BitmapFactory.decodeStream(is); } connection.disconnect(); } catch (IOException e) { e.printStackTrace(); } } catch (MalformedURLException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); mHttpHandler.sendEmptyMessage(0); } } }
布局如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="org.durian.durianhttpresponsecache.DurianMainActivity"> <Button android:id="@+id/button" android:text="button" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/reload" android:text="reload" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
这里面需要4.0版本以上的.
测试效果:
<1> : 先把设备打开网络,点击第一个按钮,显示出图片;
<2> : 再关闭网络,在点击第二个按钮,然后再点击第一个按钮;
<3> : 图片继续显示了.
打开Android studio网络监察情况:
然后关闭网络后:
图片仍然显示.
保存了缓存:
这样就可以缓解反复网络加载的情况了,节省耗电,同时快速加载了图片,体验效果得到优化.