如何在Android中解决此Web视图错误“ ERR_CONNECTION_RESET”?

时间:2019-12-16 07:02:45

标签: android webview ssl-certificate

当我加载此网址www.droponway.com到android的网络视图中时,会显示此错误 enter image description here

当我加载其他网址时,就像 www.facebook.com 一样。

Web视图代码在下面

webViewHome.loadUrl(webURL);

    webViewHome.setWebViewClient(new WebViewClient() {

        public void onPageFinished(WebView view, String url) {
            // do your stuff here
            pbProgressAction.setVisibility(View.INVISIBLE);
        }
    });

private class CustomWebViewClient extends WebViewClient
{
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        return super.shouldOverrideUrlLoading(view, request);
    }
}

我还添加了忽略SSL但没有任何反应的方法

private static void disableSSLCertificateChecking() {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        @Override
        public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
            // Not implemented
        }

        @Override
        public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
            // Not implemented
        }
    } };

    try {
        SSLContext sc = SSLContext.getInstance("TLS");

        sc.init(null, trustAllCerts, new java.security.SecureRandom());

        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (KeyManagementException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
}

2 个答案:

答案 0 :(得分:0)

我在浏览器中检查了您的网址,我认为该网站已关闭并进行了检查

答案 1 :(得分:0)

您的 https 网址似乎不是私密的。如果您想检查连接是否私密,请以隐身模式或(其他浏览器)打开浏览器并加载 URL。

如果您收到隐私警告,例如:“您的连接不是私密的” 将以下客户端添加到 webview。

private class MyWebViewClient(private val binding: FragmentSearchBinding) : WebViewClient() {
    override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
        view?.loadUrl(url!!)
        return false
    }

    override fun onPageFinished(view: WebView?, url: String?) {
        super.onPageFinished(view, url)
        binding.progressBar.visibility = View.GONE
    }

    override fun onReceivedError(
        view: WebView?,
        request: WebResourceRequest?,
        error: WebResourceError?
    ) {
        super.onReceivedError(view, request, error)
    }
    //Important part
    override fun onReceivedSslError(
        view: WebView?,
        handler: SslErrorHandler?,
        error: SslError?
    ) {
        handler?.proceed()
    }
}