WebView webView = (WebView)findViewById(R.id.webview);
// setting WebViewClient
webView.setWebViewClient(new DemoWebViewClient());
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://github.com");
DemoWebViewClient是继承WebViewClient
类的类,实现了onReceivedSslError
方法
private class DemoWebViewClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
LogUtil.d("WebView", "onPageStarted " + url);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
LogUtil.d("WebView", "in shouldOverrideUrlLoading");
view.loadUrl(url);
return true;
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
LogUtil.d("WebView", "onReceivedSslError");
handler.proceed(); // Ignore SSL certificate errors
}
@Override
public void onPageFinished(WebView view, String url) {
LogUtil.d("WebView", "onPageFinished " + url);
}
}
添加网络访问权限
<uses-permission android:name="android.permission.INTERNET" />