这是我的网页浏览:
if(bundle != null){
String file = bundle.getString("url");
Log.i("","url is:" + file);
wv.setWebViewClient(new MyWebViewClient());
wv.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
WebSettings settings = wv.getSettings();
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
settings.setAppCacheEnabled(false);
settings.setDomStorageEnabled(true);
wv.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
loadingPanel.setVisibility(View.GONE);
}
});
wv.loadUrl(file);
}
这是我的WebViewClient:
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
wv.loadUrl(url);
return true;
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler,
SslError error) {
super.onReceivedSslError(view, handler, error);
Log.e("","error is certificate:" + error.getCertificate());
Log.e("","error is:" + error.getPrimaryError());
Log.e("","error is url:" + error.getUrl());
handler.proceed();
}
}
我收到此错误:
10-12 13:27:43.174: I/X509Util(14748): Failed to validate the certificate chain, error: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
此外,未达到" onReceivedSSLError方法。我没有得到我在该函数中添加的日志"。我做错了什么?
这也是我的权限,我不知道我是否需要更多:
android.permission.INTERNET"
android.permission.READ_EXTERNAL_STORAGE"
我甚至尝试将证书添加到webview,如下所示:
try {
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
Certificate cer = keyStore.getCertificate("ca");
if(cer != null) {
Log.i("", "certificate is:" + cer);
Log.i("", "certificate is:" + cer.toString());
Log.i("", "certificate is:" + cer.getType());
Log.i("", "certificate is:" + cer.getPublicKey());
X509Certificate x509 = (X509Certificate) cer;
SslCertificate sslCert = new SslCertificate(x509);
wv.setCertificate(sslCert);
}
}catch (Exception e){
Log.e("","error trying to get it:" + e.getMessage());
}
但没有运气。
答案 0 :(得分:4)
类MyWebViewClient()
包含SSL错误处理程序。您可以在代码段的第4行中正确设置:
wv.setWebViewClient(new MyWebViewClient());
然而,在第12行中,您再次调用wv.setWebViewClient(...)
来替换先前设置的WebViewClient。因此代码永远不会被调用......