Android-HttpURLConnection自己主动管理cookie
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的更多相关文章
- Android HttpURLConnection源代码分析
Android HttpURLConnection源代码分析 之前写过HttpURLConnection与HttpClient的差别及选择.后来又分析了Volley的源代码. 近期又遇到了问题,想在V ...
- Android HttpURLConnection.connect找不到源 HttpURLConnection连接失败 HttpURLConnection.connect IO异常 解决办法
Android HttpURLConnection.connect找不到源 HttpURLConnection连接失败 HttpURLConnection.connect IO异常 解决办法 以下代 ...
- android 给url添加cookie
前些天因为项目需要写了一个通过网络连接去服务端拿数据的方法,但是需要让程序添加上cookie,因为之前对cookie 没有怎么研究过(包括做web 那会也没有用过或者说很少用),所以 一时用起来不太会 ...
- android——HttpUrlConnection
前面了解了下服务端和客户端的相关知识 ,那么他们是通过什么来进行进行连接的呢? Android可以用HttpURLConnection或HttpClient接口来开发http程序.在Android 上 ...
- [Android] HttpURLConnection & HttpClient & Socket
Android的三种网络联接方式 1.标准Java接口:java.net.*提供相关的类//定义地址URL url = new URL("http://www.google.com" ...
- Android HttpURLConnection Post 参数 (https)
声明utf-8: public static String CHARSET_UTF8 = HTTP.UTF_8; eg:登陆请求方法,通过接口返回结果: public static void logi ...
- Android HttpURLConnection And HttpClient
Google的工程师的一个博客写到: HttpURLConnection和HttpClient Volley HTTP请求时:在Android 2.3及以上版本,使用的是HttpURLConnecti ...
- android webview setcookie 设置cookie
CookieSyncManager.createInstance(mWebView.getContext()); CookieManager cookieManager = CookieManager ...
- Android HttpURLConnection的使用+Handler的原理及典型应用
1.介绍 总结:HttpURLConnection用来发送和接收数据. 2.ANR异常报错 (1)ANR(Application not response) 应用无响应, 主线程(UI线程) (2)如 ...
- Android 给WebView设置Cookie
最近项目中用到WebView访问新浪支付页面,有个要求是必须是登录状态,否则会报Token过期,然后我简单的将我从cookie中取得的ticket,即一串数字可以代表用户登录的唯一标识作为参数拼接到u ...
随机推荐
- java 基本数据类型及自己主动类型提升
基本数据类型:8种 1.整型: byte 1个字节 8位 -128到127 short 2个字节 16位 -2^15到(2^15)-1 int 4个字节 32 ...
- 派生类地址比基类地址少4(CDerived对象的起始地址存放的是虚表指针vptr,也就是子类的第一项内容。接下来的是基类的成员变量,接下来再是自身的成员变量)
大家对虚表并不陌生,都知道每个含有虚函数的类对象都有1个虚指针,但是在现实使用中,却总是因为这而调试半天,才发现原来是虚指针惹的祸.我这几天在调试代码时候也中招了,我的问题是这样的,如下图,CTree ...
- File and Folder Permissions
https://msdn.microsoft.com/en-us/library/bb727008.aspx On NTFS volumes, you can set security permiss ...
- 【BZOJ 2821】作诗
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2821 [算法] 如果不强制在线,显然莫队是可以解决此题的,那么,强制在线怎么办呢? ...
- eclipse搭建android开发环境
1.首先安装JDK 此步骤是做JAVA必经之路,不多累述,强调要注意的地方: 目前为止android的开发环境只支持JDK1.7,千万不要下载JDK1.8. 下载的JDK一定要选择好操作系统,特别是要 ...
- ASP.net 中 OutputCache 指令各个参数的作用
使用@ OutputCache指令 使用@ OutputCache指令,能够实现对页面输出缓存的一般性需要.@ OutputCache指令在ASP.NET页或者页中包含的用户控件的头部声明.这种方式非 ...
- LeetCode224. Basic Calculator (用栈计算表达式)
解题思路 用两个栈分别存字符和数字. 顺序读入字符,处理方式分为字符和数字两种. 处理字符分为')'和非')'两种. 处理数字需要读取字符栈栈顶,分为'+'.'-'和非'+'.'-'. 代码 clas ...
- Pyhton学习——Day5
# s=set('hello')# print(s)## s=set(['alex','alex','sb'])# print(s) # s={1,2,3,4,5,6} #添加# s.add('s') ...
- EntityFramework 二
特性 用来具体的设置数据库属性 [Table("表名")]//设置表名 public class User { [Key] //设置主键 [Column("列名&qu ...
- css——导航栏
导航栏一般用无序列表制作 但出来的导航栏有黑点,还有一些边距 去除黑点我们可以用:list-style-type: none;/*去掉ul前面的点*/ 因为有些标签之间会有默认的边距,所以可以先将边踞 ...