HttpResponseCache的使用 缓存 cache

HttpResponseCache提供了一种方便的方式来缓存HTTP响应,节省电量并减少网络请求。适用于小数据量的缓存,不推荐用于大量图片。在Application中启用后,HttpURLConnection会自动处理缓存,每个缓存文件包含数据文件和HTTP头部信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前我们在软件开发中,cache都是自己来写,不管是图片缓存还是其他从网络获取的数据,有了HttpResponseCache,它帮助我们可以很好的解决cache这个问题(我现在感觉他只适合cache一些小的数据,如果大量的图片cache还是自己缓存到SD卡上面去比较好)。

HttpResponseCache的好处:

1.明显一点节约电,减少了网络请求。

2.开发者不用自己在去写cache机制了。

3.最根本的一点就是,如果开发者在开发中不是使用的HttpClient, HttpDefaultClient..., 而是用 HttpURLConnection的话, 你根本不用改本來的 Code。

这个我们就不多说了,直接看示例:

在开发中你不用写其他任何东西,只要在Application层将其启动就好了 其他的全部交给HttpURLConnection处理就行。

public class HttpCacheApplication extends Application {
	@Override
	public void onCreate() {
		super.onCreate();
		new Thread() {
			@Override
			public void run() {
				enableHttpResponseCache();
			}
		}.start();
	}

	private void enableHttpResponseCache() {
		try {
			long httpCacheSize = 10 * 1024 * 1024;// 10M
			File httpCacheDir = new File(getCacheDir(), "http");
			Class.forName("android.net.http.HttpResponseCache")
					.getMethod("install", File.class, long.class)
					.invoke(null, httpCacheDir, httpCacheSize);
		} catch (Exception e) {
			Log.e("===>", e.getMessage(), e);
		}
	}

}

接下来我们来看看HttpUrlConnection是怎么处理的,怎么缓存的。

public class MainActivity extends Activity {

	private final String TAG = getClass().getSimpleName();
	ImageView img;
	Button msg;
	TextView tv;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		img = (ImageView) findViewById(R.id.imageView1);
		tv = (TextView)findViewById(R.id.textView1);
		msg = (Button) findViewById(R.id.button1);
		msg.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				new InternetTask().execute();
			}
		});
		findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				MainActivity.this.finish();
			}
		});
	}

	class InternetTask extends AsyncTask<String, String, Boolean> {
		Bitmap bitmap;
		String jsonStr;

		@Override
		protected void onPostExecute(Boolean result) {
			super.onPostExecute(result);
			img.setImageBitmap(bitmap);
			tv.setText(jsonStr);
		}

		@Override
		protected Boolean doInBackground(String... params) {
			// Test download image
			try {
				URL url = new URL("http://news.baidu.com/resource/img/logo_news_137_46.png");
				HttpURLConnection conn = (HttpURLConnection) (url
						.openConnection());
				conn.connect();
				InputStream is = conn.getInputStream();
				BitmapFactory.Options ops = new BitmapFactory.Options();
				bitmap = BitmapFactory.decodeStream(is, null, ops);
				is.close();
				conn.disconnect(); 
			} catch (Exception e) {
				Log.e(TAG, e.getMessage(), e);
			}

			// Test download JSON data
			try {
				URL url = new URL("http://www.baidu.com/");
				HttpURLConnection conn = (HttpURLConnection) (url
						.openConnection());
				conn.connect();  
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(conn.getInputStream(), "UTF-8"));
				jsonStr = reader.readLine();
				InputStream is = conn.getInputStream();
				is.close();
				conn.disconnect();
			} catch (Exception e) {
				Log.e(TAG, e.getMessage(), e);
			}
			return true;
		}

	}

}
我们看下效果:

看下缓存文件,每个文件会产生两个文件,一个是数据文件,一个是http header 信息


接下来将说下各个链接代理怎么使用

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值