Android 4.0 设置Http缓存的方式

android 4.0加入了HttpResponseCache这个类来设置缓存.使用它的方式有2种:

1.

		try {
			// 单位KB
			long httpCacheSize = 10 * 1024 * 1024;
			// 缓存位置
			File httpCacheDir = new File(getActivity().getCacheDir(), "http");
			// 反射调用
			Object result = Class.forName("android.net.http.HttpResponseCache")
					.getMethod("install", File.class, long.class)
					.invoke(null, httpCacheDir, httpCacheSize);
			if (result instanceof HttpResponseCache) {
				//设置为系统默认缓存
				HttpResponseCache cacheObj = (HttpResponseCache) result;
				Class.forName("java.net.ResponseCache")
						.getMethod("setDefault", ResponseCache.class)
						.invoke(null, cacheObj);
			}
		} catch (Exception httpResponseCacheNotAvailable) {
			httpResponseCacheNotAvailable.printStackTrace();
		}

2.

		if (VERSION.SDK_INT >= 14) {
			try {
				// 单位KB
				long httpCacheSize = 10 * 1024 * 1024;
				// 缓存位置
				File httpCacheDir = new File(getActivity().getCacheDir(),
						"http");
				ResponseCache.setDefault(HttpResponseCache.install(
						httpCacheDir, httpCacheSize));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
怎么样 很简单吧):
下一篇: Android 基于TCP的通信流程设计(金融行情)
糊涂无际
博客等级 码龄15年 1粉丝 5原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值