webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//传入cookie
synCookies(ShebaoWebActivity.this, url);
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
});
传入cookie
/**
* 同步一下cookie
*/
public static void synCookies(Context context, String url) {
try {
if (Build.VERSION.SDK_INT < 21) {
CookieSyncManager.createInstance(context);
}
CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
if (Build.VERSION.SDK_INT >= 21) {
cookieManager.setAcceptThirdPartyCookies(webView, true);
}
cookieManager.removeSessionCookie();
cookieManager.removeAllCookie();//有延时,有些手机无法及时清空
SystemClock.sleep(500);
cookieManager.setCookie(url, cookies);
if (Build.VERSION.SDK_INT < 21) {
CookieSyncManager.getInstance().sync();
} else {
CookieManager.getInstance().flush();
}
} catch (Exception e) {
e.printStackTrace();
}
}