华为系统更新后安装了一个谷歌6月安全补丁的东西,然后之前写的调h5页面的部分就出现了问题,后台查过发现是Android端调h5页面时cookie没能带过去,导致了登录失败。于是对setCookie部分的代码进行了调试,发现单步运行时只要在setCookie部分时多等一会页面就可以正常打开,所以怀疑是异步的问题,先是在网上查了一通,说是这种情况可以让程序多睡会,但是这种解决方法并没有成功,而且觉得这么写代码有点别扭,于是就又查了一下设置Cookie部分的官方说明,发现 cookieManager.removeAllCookies其实是异步函数,之前写代码的人把他当同步用了,导致setCookie完成后,removeAllCookies函数才执行完的,把填进去的cookie又给移除了,所以导致了打开页面的失败。没安补丁之前,可能是cookie都带过去了,remove函数才执行完,安补丁后remov函数的运行速度提升了,但是具体原因不明,因为查不到补丁的内容,总之无论如何,异步函数当同步使用是万万不能的。

以下是改之前错误的代码,removeAllCookies被当成了同步函数使用

void fun(){
...
addCookies();
mWebview = (SystemWebView) appView.getView();
engine = (SystemWebViewEngine) appView.getEngine();
mWebview.setWebViewClient(new SystemWebViewClient(engine){
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}

...
});
}

private void addCookies() {
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
cookieManager.setAcceptCookie(true);
List<Cookie> cookieList = MainApp.getInstance().getGlobalState().getCookies();
for (Cookie cookie: cookieList){
String cookieString = cookie.name() + "=" + cookie.value();
cookieManager.setCookie(url, cookieString);
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.flush();
} else {
CookieSyncManager.createInstance(this);
CookieSyncManager.getInstance().sync();
}
}
以下是改后的代码,removeAllCookies()有一个完成后会在当前线程调用的方法,利用他remove完成后在set就没问题了,而且addCookie()的调用被移进了onPageStarted之前,不然,之前那么写的话会先完成调用onPageStarted(),后完成addCookie()的调用,cookie依然不能设置成功。addCookie()不能放在onPageStarted中,应该加载完cookie再加载url,在loadUrl之前调用。

void fun(){
mWebview = (SystemWebView) appView.getView();
engine = (SystemWebViewEngine) appView.getEngine();
mWebview.setWebViewClient(new SystemWebViewClient(engine){
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// addCookies();
super.onPageStarted(view, url, favicon);
}

...
});

}

private void addCookies() {
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.removeAllCookies(new ValueCallback<Boolean>() {
@Override
public void onReceiveValue(Boolean value) {
setCookie(cookieManager);
}
});
}else{
cookieManager.removeAllCookie();
setCookie(cookieManager);
}
}

void setCookie( CookieManager cookieManager){

List<Cookie> cookieList = MainApp.getInstance().getGlobalState().getCookies();
for (Cookie cookie: cookieList){
String cookieString = cookie.name() + "=" + cookie.value();
cookieManager.setCookie(url, cookieString);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.flush();
} else {
CookieSyncManager.createInstance(this);
CookieSyncManager.getInstance().sync();
}

loadUrl();
}
 这是removeAllCookies()方法的完整说明

/**
* Removes all cookies.
* <p>
* This method is asynchronous.
* If a {@link ValueCallback} is provided,
* {@link ValueCallback#onReceiveValue(T) onReceiveValue()} will be called on the current
* thread's {@link android.os.Looper} once the operation is complete.
* The value provided to the callback indicates whether any cookies were removed.
* You can pass {@code null} as the callback if you don't need to know when the operation
* completes or whether any cookies were removed, and in this case it is safe to call the
* method from a thread without a Looper.
* @param callback a callback which is executed when the cookies have been removed
*/
public abstract void removeAllCookies(ValueCallback<Boolean> callback);

cookie加载不正确的问题的更多相关文章

  1. VS2010打不开VS2012 .NET MVC 工程,及打开后部分模块加载不正确的解决办法

    转自http://www.xuebuyuan.com/2042634.html 首先,如果sln打开不正确,用(notepad++)打开sln 比如 VS2010的前两行为: Microsoft Vi ...

  2. [dpdk][kernel][driver] 如何让DPDK的UIO开机自动加载到正确的网卡上

    0. 前言 开了虚拟机,开始dpdk之前,我每天都干这几件事: [root@dpdk potatos]# modprobe uio [root@dpdk potatos]# insmod /root/ ...

  3. C# — 调用dll出现试图加载不正确格式的程序问题

    今天在调用百度dll包时,运行项目出现了如下警告: 修改:鼠标右击项目名称----选择属性----生成-----平台目标-----X64(由于我调用的是X64的dll包,所以这里选择X64,网上许多说 ...

  4. c++ clr编译dll在c#调用时出现“试图加载不正确的格式”“找不到dll”错误的解决

    用depends发现缺了一堆API-MS-WIN什么的dll,网上查找是因为少了VC++2010,VC++2015等一系列,装好后仍然不行,原来这种错误并不是该原因导致的,也并不缺少那些dll(dep ...

  5. iOS “请在微信客户端打开链接” UIWebview加载H5页面携带session、cookie、User-Agent信息 设置cookie、清除cookie、设置User-Agent

    公司新开的一个项目..内容基本上是加载H5页面显示..当时觉得挺简单的..后来发现自己掉坑里了..一些心理历程就不说了..说这个项目主要用到的知识点吧..也是自己踩得坑. 首先说说..这个项目上的内容 ...

  6. AngularJs 动态加载模块和依赖

    最近项目比较忙额,白天要上班,晚上回来还需要做Angular知识点的ppt给同事,毕竟年底要辞职了,项目的后续开发还是需要有人接手的,所以就占用了晚上学习的时间.本来一直不打算写这些第三方插件的学习笔 ...

  7. C/C++ http协议加载sessionID

    很多时候,使用一个既定的API虽然可以去完成一些任务,但是还是不如自己写来的那么随性和自由. http协议,最常用的get,post两种方式传输数据,关于post,有时候,需要用到加载cookie信息 ...

  8. PHP中的自动加载

      自动加载? 或许你已经对自动加载有所了解.简单描述一下:自动加载就是我们在new一个class的时候,不需要手动去写require来导入这个class.php文件,程序自动帮我们加载导入进来.这是 ...

  9. Windbg SOS 加载技巧(.net framwork 2.0)

    1.打开windbg,加载dump.使用命令确定dump的clr版本:lm vm mscorwks 或者lm vm clr(!eeversion可以查看加载后的sos版本) 2.找到对应的mscorw ...

随机推荐

  1. java设计模式解析(1) Observer观察者模式

      设计模式系列文章 java设计模式解析(1) Observer观察者模式 java设计模式解析(2) Proxy代理模式 java设计模式解析(3) Factory工厂模式 java设计模式解析( ...

  2. nodejs查看本机hosts文件域名对应ip

    const dns = require('dns') dns.lookup('domainName', function(err, result) { console.log(result) }) r ...

  3. 集合(Collection)类

    集合(Collection)类是专门用于数据存储和检索的类.这些类提供了对栈(stack).队列(queue).列表(list)和哈希表(hash table)的支持.大多数集合类实现了相同的接口. ...

  4. Python中对列表排序实例

    Python中对列表排序实例 发布时间:2015-01-04 09:01:50 投稿:junjie 这篇文章主要介绍了Python中对列表排序实例,本文给出了9个List的排序实例,需要的朋友可以参考 ...

  5. 解决Cannot find config.m4 Make sure that you run '/home/php/bin/phpize' in the top level source directory of the module

    oot@DK:/home/daokr/downfile/php-7.0.0/ext/mysqlnd# /home/php/bin/phpizeCannot find config.m4. Make s ...

  6. USACO10FEB]慢下来Slowing down dfs序 线段树

    [USACO10FEB]慢下来Slowing down 题面 洛谷P2982 本来想写树剖来着 暴力数据结构直接模拟,每头牛回到自己的农场后,其子树下的所有牛回到农舍时,必定会经过此牛舍,即:每头牛回 ...

  7. Open Judge 1.4 09

    09:判断能否被3,5,7整除 总时间限制:  1000ms 内存限制:  65536kB 描述 给定一个整数,判断它能否被3,5,7整除,并输出以下信息:1.能同时被3,5,7整除(直接输出3 5 ...

  8. 2017.10.7 国庆清北 D7T1 计数

    题目描述 给出m个数a[1],a[2],…,a[m] 求1~n中有多少数不是a[1],a[2],…,a[m]的倍数. 输入输出格式 输入格式: 输入文件名为count.in. 第一行,包含两个整数:n ...

  9. 最长公共子序列 DP

    class Solution: def LCS(self,A,B): if not A or not B: #边界处理 return 0 dp = [[0 for _ in range(len(B)+ ...

  10. windows传文件到linux服务器--- secureCRT PK xftp

    背景: 需要从windows上传下载文件到aws虚拟服务器上并进行服务器环境搭建,由于secureCRT的局限性它只支持pub格式的密钥,不支持pem格式密钥,xshell是支持pem格式的,所以尝试 ...