饼干和Webview - Android 中的 CookieSyncManager! [英] Cookies & Webview - CookieSyncManager in Android!

查看:25
本文介绍了饼干和Webview - Android 中的 CookieSyncManager!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一项活动可让您登录页面.在下一个活动中,如果登录成功,它应该显示基于 cookie 的网页.cookie 被检索,我尝试使用以下代码将其放在 webView 上:

 Cookie sessionCookie = LoginWebView.cookie;CookieSyncManager.createInstance(webview.this);CookieManager cookieManager = CookieManager.getInstance();如果(sessionCookie != null){cookieManager.removeSessionCookie();String cookieString = sessionCookie.getName() + "=" + sessionCookie.getValue() + "; domain=" + sessionCookie.getDomain();Log.v(TAG, "COOKIE SYNC:" + cookieString);cookieManager.setCookie(域, cookieString);CookieSyncManager.getInstance().sync();}webView.setWebViewClient(new MyWebViewClient());webView.loadUrl("http://a_page.com/getpageiphone.aspx?p=home");

这是基于 StackOverflow 上其他问题的代码,但当我加载网页时,它似乎不起作用.我的代码似乎有问题,但我看不到哪里,我开始认为我做错了什么.

解决方案

您已经使用了这一行 -

 if (sessionCookie != null) {cookieManager.removeSessionCookie();}

.确保您每次都能收到新的 cookie.

好像你遇到了和我一样的问题,请查看下面的链接 -

android (code.google.com) 的removeSessionCookie() 问题>

它说removeSessionCookie()是在一个线程中实现的,所以每当它被调用时;一个线程启动,在您的 setCookie(url, cookieString); 被调用后,它会删除您刚刚设置的新 cookie.因此,对于某些设备,它运行良好,因为 removeSessionCookie() 已经被执行,而对于某些设备,它删除了 cookie,我们就遇到了这个问题.

通过使用 SystemClock.sleep(500); ,你只是让系统先完成 removeSessionCookie()

我建议你删除这个 removeSessionCookie(); 因为你只设置了一个 cookie,所以它不会与其他 cookie 冲突.您的代码将无缝运行.

I have an activity that lets you sign in to a page. In the next activity it should display a webpage based on the cookie if the login was successful. The cookie is retrived and I try to put it on the webView with the following code:

    Cookie sessionCookie = LoginWebView.cookie;
    CookieSyncManager.createInstance(webview.this);
    CookieManager cookieManager = CookieManager.getInstance();
    if (sessionCookie != null) {
                        cookieManager.removeSessionCookie();
        String cookieString = sessionCookie.getName() + "=" + sessionCookie.getValue() + "; domain=" + sessionCookie.getDomain();
        Log.v(TAG, "COOKIE SYNC: " + cookieString);
        cookieManager.setCookie(domain, cookieString);
        CookieSyncManager.getInstance().sync();

    }

    webView.setWebViewClient(new MyWebViewClient ());
    webView.loadUrl("http://a_page.com/getpageiphone.aspx?p=home");

This is losely based on code from other questions here on StackOverflow, but when I load the web-page it does not seem to work. It seems as there is something very wrong with my code but I can't see where and I'm starting to think I'm doing something very wrong.

解决方案

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() first

I 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.

这篇关于饼干和Webview - Android 中的 CookieSyncManager!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆