Volley那么好用的框架居然没有内置对cookie的处理,自己搞一个!
public class MobCookieManager {//转载请标明出处:http://blog.csdn.net/goldenfish1919/article/details/46890245

	private MobCookieManager(){}

	/**
* 应用启动的时候调用,參考:{@link CookieManager#getInstance CookieManager.getInstance()}
* */
public static void init(Context context){
CookieSyncManager.createInstance(context);
} public static String getCookie(String url){
CookieManager cookieManager = CookieManager.getInstance();
return cookieManager.getCookie(url);
} /**
* http://stackoverflow.com/questions/16007084/does-android-webkit-cookiemanager-works-on-android-2-3-6
* */
public static void setCookies(String url, Map<String, List<String>> headerFields) {
if (null == headerFields) {
return;
}
List<String> cookies = headerFields.get("Set-Cookie");
if (null == cookies) {
return;
}
CookieSyncManager.getInstance().startSync();
for (String cookie : cookies) {
setCookie(url, cookie);
}
CookieSyncManager.getInstance().sync();
} private static void setCookie(String url, String cookie) {
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true); if(cookie.indexOf("Expires") < 0){
cookie = addExpireToCookie(cookie);
}
cookieManager.setCookie(url, cookie);
} /**
* http://stackoverflow.com/questions/8547620/what-is-a-session-cookie
* */
private static String addExpireToCookie(String cookie) {
Date expireDate = new Date(new Date().getTime() + 24L*60*60*1000);
String datestr =DateUtil.format(DateUtil.east8ToGmt(expireDate), DateUtil.FORMAT_GMT);
String arr[] = cookie.split(";");
StringBuilder sb = new StringBuilder();
sb.append(arr[0]);
sb.append("; ").append("Expires=").append(datestr);
if(arr.length > 1){
for(int i=1; i<arr.length; i++){
sb.append(";").append(arr[i]);
}
}
return sb.toString();
} }
</pre><pre name="code" class="java"><pre name="code" class="java">public class DateUtil {

	public static final String FORMAT_MDHM = "MM-dd HH:mm";
public static final String FORMAT_YMD = "yyyy-MM-dd";
public static final String FORMAT_YMDHM = "yyyy-MM-dd HH:mm";
public static final String FORMAT_YMDHMS = "yyyy-MM-dd HH:mm:ss";
public static final String FORMAT_GMT = "EEE, dd-MMM-yyyy HH:mm:ss 'GMT'"; private static final String TAG = DateUtil.class.getSimpleName();
private static final Locale DEFAULT_LOCALE = Locale.CHINA; private static ThreadLocal<Map<String, SimpleDateFormat>> threadLocal = new ThreadLocal<Map<String, SimpleDateFormat>>() {
protected synchronized Map<String, SimpleDateFormat> initialValue() {
Map<String, SimpleDateFormat> map = new HashMap<String, SimpleDateFormat>();
map.put(FORMAT_MDHM, new SimpleDateFormat(FORMAT_MDHM, DEFAULT_LOCALE));
map.put(FORMAT_YMD, new SimpleDateFormat(FORMAT_YMD, DEFAULT_LOCALE));
map.put(FORMAT_YMDHM, new SimpleDateFormat(FORMAT_YMDHM, DEFAULT_LOCALE));
map.put(FORMAT_YMDHMS, new SimpleDateFormat(FORMAT_YMDHMS, DEFAULT_LOCALE));
map.put(FORMAT_GMT, new SimpleDateFormat(FORMAT_GMT, DEFAULT_LOCALE));
return map;
}
}; private DateUtil(){} public static SimpleDateFormat getDateFormat(String format) {
Map<String, SimpleDateFormat> map = (Map<String, SimpleDateFormat>) threadLocal.get();
SimpleDateFormat sdf = map.get(format);
if(sdf != null){
return sdf;
}
try{
sdf = new SimpleDateFormat(format, DEFAULT_LOCALE);
map.put(format, sdf);
}catch(Exception e){
MyLog.e(TAG, e);
}
return sdf;
} public static Date parse(String textDate, String format) {
if(textDate == null || textDate.length() <= 0){
return null;
}
try{
SimpleDateFormat sdf = getDateFormat(format);
if(sdf == null){
return null;
}
return sdf.parse(textDate);
}catch(Exception e){
MyLog.e(TAG, e);
return null;
} } public static String format(Date date, String format){
if(date == null){
return null;
}
SimpleDateFormat sdf = getDateFormat(format);
if(sdf == null){
return null;
}
return sdf.format(date);
} public static Date east8ToGmt(Date src){
if(src == null){
return null;
}
TimeZone srcTimeZone = TimeZone.getTimeZone("GMT+8");
TimeZone destTimeZone = TimeZone.getTimeZone("GMT");
long targetTime = src.getTime() - srcTimeZone.getRawOffset() + destTimeZone.getRawOffset();
return new Date(targetTime);
} }

注意:我们这里使用的android.webkit.CookieManager。



Android-HttpURLConnection自己主动管理cookie的更多相关文章

  1. Android HttpURLConnection源代码分析

    Android HttpURLConnection源代码分析 之前写过HttpURLConnection与HttpClient的差别及选择.后来又分析了Volley的源代码. 近期又遇到了问题,想在V ...

  2. Android HttpURLConnection.connect找不到源 HttpURLConnection连接失败 HttpURLConnection.connect IO异常 解决办法

    Android HttpURLConnection.connect找不到源  HttpURLConnection连接失败 HttpURLConnection.connect IO异常 解决办法 以下代 ...

  3. android 给url添加cookie

    前些天因为项目需要写了一个通过网络连接去服务端拿数据的方法,但是需要让程序添加上cookie,因为之前对cookie 没有怎么研究过(包括做web 那会也没有用过或者说很少用),所以 一时用起来不太会 ...

  4. android——HttpUrlConnection

    前面了解了下服务端和客户端的相关知识 ,那么他们是通过什么来进行进行连接的呢? Android可以用HttpURLConnection或HttpClient接口来开发http程序.在Android 上 ...

  5. [Android] HttpURLConnection & HttpClient & Socket

    Android的三种网络联接方式 1.标准Java接口:java.net.*提供相关的类//定义地址URL url = new URL("http://www.google.com" ...

  6. Android HttpURLConnection Post 参数 (https)

    声明utf-8: public static String CHARSET_UTF8 = HTTP.UTF_8; eg:登陆请求方法,通过接口返回结果: public static void logi ...

  7. Android HttpURLConnection And HttpClient

    Google的工程师的一个博客写到: HttpURLConnection和HttpClient Volley HTTP请求时:在Android 2.3及以上版本,使用的是HttpURLConnecti ...

  8. android webview setcookie 设置cookie

    CookieSyncManager.createInstance(mWebView.getContext()); CookieManager cookieManager = CookieManager ...

  9. Android HttpURLConnection的使用+Handler的原理及典型应用

    1.介绍 总结:HttpURLConnection用来发送和接收数据. 2.ANR异常报错 (1)ANR(Application not response) 应用无响应, 主线程(UI线程) (2)如 ...

  10. Android 给WebView设置Cookie

    最近项目中用到WebView访问新浪支付页面,有个要求是必须是登录状态,否则会报Token过期,然后我简单的将我从cookie中取得的ticket,即一串数字可以代表用户登录的唯一标识作为参数拼接到u ...

随机推荐

  1. memcached—向memcached中保存Java实体需注意的问题

    今天以代码实例的形式总结一下向memcached中保存Java实体需注意的问题: memcached工具类代码: package com.ghj.packageoftool; import java. ...

  2. 多线程的join和interrupt

    你可以在一个线程1里添加线程2对象thread的join方法来让线程1处于等待的状态 ,同时也可以调用thread.interrupt()来打断等待状态,此处注意 interrupt应在线程1开启st ...

  3. Android中XML解析,保存的三种方法

    简单介绍 在Android开发中,关于XML解析有三种方式,各自是: SAX 基于事件的解析器.解析速度快.占用内存少.非常适合在Android移动设备中使用. DOM 在内存中以树形结构存放,因此检 ...

  4. tcMalloc 配置和优化 nginx 高性能

    tcMalloc优化nginx  记住:nginx一定要先启动 1>下载安装libunwind: #wget  http://download.savannah.gnu.org/releases ...

  5. SecureCRT学习之道:SecureCRT 经常使用技巧

    快捷键: 1. ctrl + a :  移动光标到行首 2. ctrl + e :移动光标到行尾 3. ctrl + d :删除光标之后的一个字符 4. ctrl + w : 删除行首到当前光标所在位 ...

  6. 2015.06.11,技术,关于Matlab中的Jbtest检验

    总体分布的正态性检验一般采取Jarque-Bera检验方法. 1. JBTest检验的定义: 在统计学中,Jarque-Bera检验是对样本数据是否具有符合正态分布的偏度和峰度的拟合优度的检验.该检验 ...

  7. BZOJ 1790: [Ahoi2008]Rectangle 矩形藏宝地

    BZOJ 1790: [Ahoi2008]Rectangle 矩形藏宝地 题目传送门 [题目大意] 游戏的主办方把这块开阔地当作第一象限,将所有可能埋藏宝藏的地方划成一个个矩形的土地,并把这些矩形土地 ...

  8. ros中文术语表及消息类型表

    前言:整理一些ros常用表格,包括中文术语对照表. 一.中文术语表 二.消息类型表 -END-

  9. 【转】C# ABP WebApi与Swagger UI的集成

    以前在做WebAPI调用测试时,一直在使用Fiddler测试工具了,而且这个用起来比较繁琐,需要各种配置,并且不直观,还有一点是还得弄明白URL地址和要传递的参数,然后才能调用.  最近新入职,公司里 ...

  10. Web前端必须规避的8个误区

    现在,有越来越多所谓的“教程”来帮助我们提高网站的易用性.下面收集了一些在Web开发中容易出错和被忽略的小问题,并且提供了参考的解决方案,以便于帮助Web开发者更好的完善网站. 通过避免下面这些小错误 ...