android上WebView设置cookie,以及设置webview cookie在部分手机失效
来源:互联网 发布:网络被攻击了怎么办 编辑:程序博客网 时间:2024/06/11 21:19
转自:http://blog.csdn.net/b275518834/article/details/51004237
类似参考:http://blog.sina.com.cn/s/blog_623868100101jlxz.html
这是在网上抄的cookie设置方案,在android 5.0系统上测试时正常在,在部分4.X手机上测试有时会失效
(我使用的测试机为中兴 ZTE android版本4.3 )
最终解决方案是
http://code.walletapp.net/post/46414301269/passing-cookie-to-webview
但是解决问题的人回答是
In few following lines I will describe my solution, how to pass cookies from DefaultHttpClient to WebView component. I was following many tutorials, but cookies weren’t send to WebView properly (in my Android 2.2). After several experiments I found out that it is working, when we wait a little time between removeSessionCookie and setCookie methods. I don’t know why. But important is, that it is working right now for me.
在removeSessionCookie和setCookie方法之间需要加入等待时间,设置cookie成功,但是不知道为什么。
解决方案是等待了1000毫秒,实际应该不需要这么长的时间
我们去android 源码在线网站上看看
http://grepcode.com/project/repository.grepcode.com/java/ext/com.google.android/android/
在android 4.0上removeSeesionCookie的代码 开启了AsyncTask的异步任务处理cookie。也就是说removeSeesionCookie这段代码是在4.0的系统上异步的
所以cookieManager.removeSeesionCookie() 异步会导致删除setcookie的内容,所以有时成功有时失败
另外4.X的CookieManager的sync()也是异步的 以下是代码段
开启一个mHandler(SyncHandler)等待100毫秒 调用 syncFlushCookieStor()->flushCookieStore()->nativeFlushCookieStore() (这句才是真正将cookiet同步的代码)
在android5.0系统上Sync回调用flush()方法
另外android 5.0官方文档上面也说明了
The WebView now automatically syncs cookies as necessary. You no longer need to create or use the CookieSyncManager. To manually force a sync you can use the CookieManager method
which is a synchronous replacement for CookieManager.flush()
.sync()
webview现在必须自动同步cookie,你不再需要创建或使用cookiesyncmanager,你可以使用CookieManager的CookieManager.flush()方法,flush()是sync()同步置换
结论在android4.X系统下面cookiesyncmanager的方法有些事异步的,removeSeesionCookie()和sync()要等待100毫秒。
而在android5.0以上的系统Cookie是同步的,sync=flush方法,调用CookieManager.flush()就是同步设置cookie。
- android上WebView设置cookie,以及设置webview cookie在部分手机失效
- android上WebView设置cookie,以及设置webview cookie在部分手机失效
- Android在webview上设置cookie,部分cookie失效的问题
- 【Android】WebView设置Cookie
- android webview 设置 Cookie
- Android WebView设置cookie
- 在Android WebView中设置Cookie
- android webview里设置cookie
- android webview里设置cookie
- android webview里设置cookie
- android webview里设置cookie
- iOS 设置webView cookie
- webView设置cookie
- webView设置Cookie
- WebView设置Cookie
- Android webview 写入cookie的解决方法以及一些属性设置
- 在Android的WebView中给一级域名设置cookie
- Android(安卓)WebView设置cookie
- OC中块Block的用法
- POJ 3414 Pots BFS
- 【OK6410裸机程序】时钟初始化
- 263. Ugly Number
- HDU 5828 Rikka with Sequence(线段树)
- android上WebView设置cookie,以及设置webview cookie在部分手机失效
- OJ-------字符统计**(自带比较)
- 架构师之路--服务器集群搭建、管理、与快速部署
- 转载Android Audio架构
- 第一篇技术博客,人生的第一篇博客!
- 二叉排序树
- 用Java模仿银行自助存取一体机(ATM)
- NOIPの模拟_2016_8_11_t2_种树
- 原根