1.各种踩坑
1.WebView必不可少的一句
webView.setWebViewClient(new WebViewClient());
2.http和https混合支持
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
3.添加 Cookie
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.setCookie(url, cookie);
4.删除 Cookie
CookieManager cookieManager = CookieManager.getInstance(); cookieManager.removeAllCookie();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.flush();
}
else{
CookieSyncManager.createInstance(Application.getInstance());
CookieSyncManager.getInstance().sync();
}
2.调试模式
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
android.webkit.WebView.setWebContentsDebuggingEnabled(true);
WebView.setWebContentsDebuggingEnabled(true);
}
3.离线缓存
if (NetworkUtil.isConnected(mContext))
mWebView.getSettings().setCacheMode(LOAD_DEFAULT);
else
mWebView.getSettings().setCacheMode(LOAD_CACHE_ELSE_NETWORK);
webSettings.setDatabaseEnabled(true);
webSettings.setDomStorageEnabled(true);
String cacheDirPath = g
etFilesDir().getAbsolutePath() +"/webCache";
mWebView.getSettings().setDatabasePath(cacheDirPath);