• cookie加载不正确的问题


    华为系统更新后安装了一个谷歌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);
     

  • 相关阅读:
    vector.clear()无法释放内存的对策
    www.wangtam.com
    VC 控件的字体控制 若将字体设置成“宋体、仿宋—GB2312、隶书、幼圆”中的某一字体时,需将lfCharSet设置成GB2312—CHARSET才使设置的字体有效
    CPU使用率 武胜
    MySQL数据类型 转 武胜
    按小时统计的语句 转 武胜
    MYSQL 行转成列 转 武胜
    推荐一个 开源C#股票软件 武胜
    WCF传输泛型 List 对象 转 武胜
    WCF 传输大数据的问题 转 武胜
  • 原文地址:https://www.cnblogs.com/yelanggu/p/10870187.html
Copyright © 2020-2023  润新知