private class MyWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
webview.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
CookieManager cookieManager = CookieManager.getInstance();
String CookieStr = cookieManager.getCookie(url);
Log.e("sunzn", "Cookies = " + CookieStr);
super.onPageFinished(view, url);
}
}
- public static void synCookies(Context context, String url) {
- CookieSyncManager.createInstance(context);
- CookieManager cookieManager = CookieManager.getInstance();
- cookieManager.setAcceptCookie(true);
- cookieManager.removeSessionCookie();
- cookieManager.setCookie(url, cookies);
- CookieSyncManager.getInstance().sync();
- }
在android里面在调用webView.loadUrl(url)之前一句调用此方法就可以给WebView设置Cookie
注:这里一定要注意一点,在调用设置Cookie之后不能再设置
- webView.getSettings().setBuiltInZoomControls(true);
- webView.getSettings().setJavaScriptEnabled(true);
这类属性,否则设置Cookie无效。