2

我正在尝试在登录后从网页中提取 cookie 的值。我正在使用 cookie 帮助程序类,并android.webkit.CookieManager在网页完成加载后检索 cookie,即onPageFinished()

这一切正常,我可以在我的测试设备 Galaxy S2 (4.0.4)、Galaxy S3 (4.1.1) 和 HTC Explorer (2.3) 上看到 cookie 名称和值。但是,相同的代码在 Sony Xperia (2.3) 上不起作用?

我已经注销了 url 以确保我在正确的页面上工作,出于某种原因,我只从 Xperia 上的站点获得了 3 个 cookie,而我应该得到 7 个(大约)。

下面是我正在运行的代码的简化版本。感谢任何回应。提前致谢!

WebViewActivity

@Override
public void onPageFinished(WebView view, String url) {
    if(!cookieCreated){
        CookieHelper cookieHelper = new CookieHelper(getApplicationContext());

        if(cookieHelper.processCookie(url)){
            cookieCreated = true;
        }
    }
}

饼干助手

public CookieHelper(Context cont){
    this.cookieManager = CookieManager.getInstance();
}


public void processCookie(String url){
    if(manager.getCookie(url).contains("cookie_name_required")){
       Log.d(TAG, "Got cookie");
    }
    else{
       Log.d(TAG, "No cookie");
    }
}
4

1 回答 1

0
public static void setCookies(Context context) {
    //gets all cookies from the HttpClient's cookie jar


    List<Cookie> cookies = httpClient.getCookieStore().getCookies();

    if (! cookies.isEmpty()) {

        CookieSyncManager.createInstance(context);
        CookieManager cookieManager = CookieManager.getInstance();
        //sync all the cookies in the httpclient with the webview by generating cookie string

        for (Cookie cookie : cookies) {

            Cookie sessionInfo = cookie;
            String cookieString = sessionInfo.getName() + "=" + sessionInfo.getValue() + "; domain=" + sessionInfo.getDomain();
            cookieManager.setCookie("zoboon.com", cookieString);
            CookieSyncManager.getInstance().sync();
        }

    } else {

        Log.i("httpclient","has no cookies");

    }
}
于 2012-10-23T22:41:09.727 回答