解决Android Http请求处理Response Cookie时区加载过慢问题


现象:Android滑动ListView需要请求网络图片资源,有明显的卡顿感,估计与log打印的内容有关

12-04 20:21:50.277: W/HttpMethodBase(19495): Cookie rejected: "$Version=0; cur_network=; $Path=/; $Domain=.kdweibo.com". Illegal domain attribute ".iweibo.comm". Domain of origin: "iweibo.comm"
12-04 20:21:51.507: I/global(19495): Loaded time zone names for en_US in 1225ms.
12-04 20:21:51.787: I/global(19495): Loaded time zone names for en_US in 1358ms.
12-04 20:21:52.187: I/global(19495): Loaded time zone names for en_US in 1399ms.
12-04 20:21:52.347: D/dalvikvm(1524): GC_EXPLICIT freed 145K, 50% free 4412K/8711K, external 152K/532K, paused 140ms
12-04 20:21:52.697: I/global(19495): Loaded time zone names for en_US in 1189ms.
12-04 20:21:52.717: W/HttpMethodBase(19495): Cookie rejected: "$Version=0; cur_user=; $Path=/; $Domain=.iweibo.com". Illegal domain attribute ".iweibo.com". Domain of origin: "iweibo.com"
12-04 20:21:52.977: I/global(19495): Loaded time zone names for en_US in 1190ms.
12-04 20:21:52.987: W/HttpMethodBase(19495): Cookie rejected: "$Version=0; cur_network=; $Path=/; $Domain=.iweibo.com". Illegal domain attribute ".iweibo.com". Domain of origin: "iweibo.com"
12-04 20:21:53.127: D/dalvikvm(19495): GC_CONCURRENT freed 1366K, 50% free 4188K/8263K, external 6258K/7479K, paused 2ms+6ms
12-04 20:21:53.427: I/global(19495): Loaded time zone names for en_US in 1245ms.
12-04 20:21:53.437: W/HttpMethodBase(19495): Cookie rejected: "$Version=0; cur_network=; $Path=/; $Domain=.iweibo.com". Illegal domain attribute ".iweibo.com". Domain of origin: "iweibo.com"
12-04 20:21:53.827: I/global(19495): Loaded time zone names for en_US in 1115ms.
12-04 20:21:54.297: I/global(19495): Loaded time zone names for en_US in 852ms.


解决方法:禁止Cookie处理

 org.apache.commons.httpclient.methods.DeleteMethod
 org.apache.commons.httpclient.methods.GetMethod
 org.apache.commons.httpclient.methods.PostMethod;

 method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
 method.addRequestHeader("Connection", "close");

对于本地格式化时区的工具类做如下处理,缓存时区格式,避免每次都出现加载过慢


       private static Map<String, SimpleDateFormat> formatMap = new HashMap<String, SimpleDateFormat>();	
       
       /**
	 * 解决 Android时区加载过慢的问题 Loaded time zone names for en_US in 1904ms.   
	 * @see http://blog.163.com/dmg_123456/blog/static/567050632011515113530636/
	 * @param date
	 * @param format
	 * @return
	 */
	public static String getGMTFromDate(Date date, String format){
		if (date == null) {
			return null;
		}
		SimpleDateFormat sdf = formatMap.get(format);
		if (null == sdf) {
			sdf = new SimpleDateFormat(format, Locale.ENGLISH);
			sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
			formatMap.put(format, sdf);
		}
		synchronized (sdf) {
				// SimpleDateFormat is not thread safe
			return sdf.format(date);
		}
	}

	public static String getStringTypeDate(Date createdAt) {
//		SimpleDateFormat dateFormat = new SimpleDateFormat(
//				"EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
//		String date=null;
//		 date = dateFormat.format(createdAt)
//				.replace("GMT+08:00", "+0800");
//		date = dateFormat.format(createdAt)
//		.replace("CST (China)", "+0800");
//		return date;
		return getGMTFromDate(createdAt, "EEE MMM dd HH:mm:ss z yyyy");
	}
	public static String getStringTypeDatePrivate(Date createdAt) {
//		SimpleDateFormat dateFormat = new SimpleDateFormat(
//				"EEE MMM dd HH:mm:ss.SSS z yyyy", Locale.ENGLISH);
//		String date=null;
//		 date = dateFormat.format(createdAt)
//				.replace("GMT+08:00", "+0800");
//		date = dateFormat.format(createdAt)
//		.replace("CST (China)", "+0800");
//		return date;
		return getGMTFromDate(createdAt, "EEE MMM dd HH:mm:ss.SSS z yyyy");
	}

参考: 巧解Android时区加载过慢的问题 
http://blog.163.com/dmg_123456/blog/static/567050632011515113530636/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值