尝试在 webview 中加载 URL 时出错 - android

ish*_*loo 6 ssl android-webview

由于 SSL 错误,我试图在我的一项活动中加载到 web 视图中的 URL 显示为空白。

我曾尝试使用网络安全配置 XML 文件夹,但我不确定我在做什么。任何帮助将不胜感激。

在调试时,当我加载 Google.com 作为 webview 中的 URL 时,页面加载正常。然后,当我尝试搜索特定站点时,它就在那里,但是当我单击它时,我在 Android Studio 的运行日志中收到了 SSL 错误。

public class About_ALC extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_about__alc);
    final WebView webview = (WebView) findViewById(R.id.web_about_alc);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setLoadWithOverviewMode(true);
    webview.getSettings().setUseWideViewPort(true);
    webview.setWebViewClient(new WebViewClient());
    webview.setWebChromeClient(new WebChromeClient());
    webview.loadUrl("https://andela.com/alc/");
}
Run Code Online (Sandbox Code Playgroud)

我在 Android Studio 中收到的错误消息是:

“E/chromium: [ERROR:ssl_client_socket_impl.cc(947)] 握手失败;返回 -1,SSL 错误代码 1,net_error -202”

Kos*_*meg 8

您需要扩展 WebViewClient 以允许加载,尽管 ssl 错误如下。

    // A new webclient that ignore ssl errors
    private class IgnoreSSLErrorWebViewClient extends WebViewClient {
        // You can all the class anything

    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
    handler.proceed(); // When an error occurs, ignore and go on
    }
}
Run Code Online (Sandbox Code Playgroud)

该站点存在 ssl 证书验证问题。默认的 WebViewClient 在出现 ssl 验证错误时停止加载。此扩展程序将指示它忽略错误并继续。

所以换行 webview.setWebViewClient(new WebViewClient());

webview.setWebViewClient(new IgnoreSSLErrortWebViewClient());

这现在对你来说应该很好用。