饼干和放大器; web视图 - CookieSyncManager在Android中!放大器、视图、饼干、web

2023-09-04 06:46:53 作者:7个字的个性游戏名字

我有一个活动,可以让您登录到一个页面。在接下来的活动它应该显示基于该Cookie网页如果登录成功。 该Cookie retrived,我要尽量把它放在web视图具有以下code:

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");

这是losely基于其他的问题在这里的StackOverflow code,但是当我加载网页它似乎并没有工作。它似乎有一些非常错误的与我的code,但我看不到的地方,我开始觉得我做的事情非常错误的。

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

  }

。 为确保您收到新的Cookie每次。

. To ensure you receive new cookie everytime.

好像你通过同样的问题已经如我面对,请查看以下链接 -

Seems like you have gone through same issue as I faced, check below link -

removeSessionCookie()发布的Andr​​oid(code.google.com)

它说, removeSessionCookie()在一个线程中实现,所以每当它被称为;一个线程启动后您 setCookie方法(URL,cookieString); 被调用时,它会删除你刚才设置的新的cookie。 因此,对于一些设备它的工作原理以及 removeSessionCookie()已经执行,而对于一些人来说,删除cookie中,我们得到了这个问题。

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.

使用 SystemClock.sleep(500); ,你只是给系统完成removeSessionCookie()首先

by using SystemClock.sleep(500); , you just gave system to finish removeSessionCookie() first

我建议你删除此 removeSessionCookie(); 因为你是只设置一个cookie中,所以不会与其他饼干冲突。您的code无缝工作。

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.