Android webview 加载自签名的https网页显示空白

来源:互联网 发布:淘宝女包店铺简介 编辑:程序博客网 时间:2024/05/28 05:15

问题

由于最新的项目采用自签名证书,所以一开始用webview加载网页并没有显示出来,而是显示一片空白。

原因

webview默认只支持加载http和通过CA等认证机构认证的https的网页。

解决

设置webview支持https
需要在onReceivedSslError方法中加入一句handler.proceed();
并且需要将super.onReceivedSslError(view, handler, error);这句注释掉。如下:

webview.setWebViewClient(new WebViewClient() {            //打开网页时不调用系统浏览器, 而是在本WebView中显示            @Override            public boolean shouldOverrideUrlLoading(WebView view, String url) {                view.loadUrl(url);                return true;            }            @Override            public void onPageStarted(WebView view, String url, Bitmap favicon) {                super.onPageStarted(view, url, favicon);                //设定加载开始的操作            }            @Override            public void onLoadResource(WebView view, String url) {                super.onLoadResource(view, url);                //设定加载资源的操作            }            @Override            public void onPageFinished(WebView view, String url) {                super.onPageFinished(view, url);                //设定加载结束的操作            }            @Override            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {                //super.onReceivedSslError(view, handler, error);//注意:这句一定要注释掉                handler.proceed();//接受证书            }        });
1 1
原创粉丝点击