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. CentOS6.5下安装远程桌面服务端软件VNC Server

    VNC 使您能够远程訪问和控制您的计算机从还有一计算机或移动设备上,不管你在世界的不论什么地方. 常见的使用情形,包含给同事和朋友提供桌面支持.远程管理您的服务器. 将 VNC Server部署到您想 ...

  2. c8---递归

    // // main.c // 递归函数 // // Created by xiaomage on 15/6/7. // Copyright (c) 2015年 xiaomage. All right ...

  3. IOC与DI区别

    (1)IOC:控制反转,把对象创建交给spring进行配置. (2)DI:依赖注入,向类里面的属性中设置值. (3)关系:依赖注入不能单独存在,需要在IOC的基础之上完成操作.

  4. 12. Integer to Roman[M]整数转罗马数字

    题目 Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from ...

  5. scrollView中内部控件的悬停

    以下图为例,图片,红色view和蓝色view是添加在scrollView上的,向上拖动,红色view停留在屏幕顶端不动,其它的继续滚动,向下拖动后,红色view跟着下来 代码如下:(注意的是scrol ...

  6. BZOJ 2821 分块+二分

    题意: N个数,M组询问,每次问[l,r]中有多少个数出现正偶数次. 思路: 把N个数分成sqrt(n)块,预处理d[i][j]表示第i块起点到第j块末尾的答案 枚举起点i,并维护一个数组记录每个数到 ...

  7. VMware虚拟机共享文件夹

    安装好虚拟文件夹后,第二次开机时发现/mnt/hgfs目录下找不到共享的文件夹,原因是vmfg-fuse服务没有开启. 在root的配置文件中添加如下代码,设置开机自启: /usr/bin/vmhgf ...

  8. poj1050查找最大子矩阵和

    题目: To the Max   Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 48507   Accepted: 2566 ...

  9. Codeforces Round #493 (Div. 2) A. Balloons 贪心水题

    由于是输出任意一组解,可以将价值从小到大进行排序,第一个人只选第一个,第二个人选其余的.再比较一下第一个人选的元素和第二个人所选元素和是否相等即可.由于已将所有元素价值从小到大排过序,这样可以保证在有 ...

  10. CF949B A Leapfrog in the Array 思维题,推理

    题意: Dima是一名初级程序员. 在他的工作中,他经常不断地重复以下操作:从数组中删除每个第二个元素. 有一天,他对这个问题的解决方案感到厌倦,他提出了以下华丽的算法. 假设有一长度为2n的数组,最 ...