您的位置:首页 > 移动开发

android webview cookie 同步时有时候会出现同步不成功的问题解决

2015-11-19 20:58 573 查看
这个问题不是随时重现,我们在同步之前会先删除所有之前的cookies,调用:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP /*&& method != null*/){cookieManager.removeSessionCookies(new ValueCallback() {@Overridepublic void onReceiveValue(Boolean value) {Log.d(TAG, "onReceiveValue value:"+value);}});}else{ cookieManager.removeSessionCookie();}

经过查阅n多stackoverflow后发现一个答案:You have used this line - if (sessionCookie != null) { cookieManager.removeSessionCookie(); }. To ensure you receive new cookie everytime.Seems like you have gone through same issue as I faced, check below link -removeSessionCookie()
issue of android (code.google.com)it says that removeSessionCookie() is implemented in a thread, so whenever it is called; a thread starts and after your setCookie(url, cookieString); is called, it removes the new cookie you just set. So for some devices it
works well as removeSessionCookie() is already executed, while, for some, it remove the cookie, and we get that problem.by using SystemClock.sleep(500); , you just gave system to finish removeSessionCookie() firstI suggest you remove this removeSessionCookie();
as you are setting only one cookie, so it won't conflict with other cookies. Your code will work seamlessly.

大概意思就是说, cookieManager.removeSessionCookie()这个方法是一个异步的方法,在删除还没有结束的时候,主线程调用CookieSyncManager.getInstance().sync();方法去同步,此时,removeSessionCookie()并没有删除结束,顺带就把刚刚同步过去的session给删除了,所以导致的同步失效,哎呀,发现问题,那么好解决了,在removeSessionCookie() 的时候SystemClock.sleep(500);ok!不会再出现烦人的同步失败重定向了。如果还有更好的方法希望给我留言,谢谢了!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: