尝试在WebView中加载URL时出错-Android [英] Error while trying to load a URL in webview - android

查看:365
本文介绍了尝试在WebView中加载URL时出错-Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在其中一项活动中加载到Web视图中的URL由于SSL错误而显示为空白.

The URL, I'm trying to load in a webview in one of my activities shows blank because of an SSL error.

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

I have tried working with the network security configuration XML folder, but I'm not sure I understand what I'm doing. Any help would be greatly appreciated.

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

While debugging, when I load Google.com as the URL in the webview, the page loads fine. Then, when I try to search for the particular site, it's there, but when I click on it I get an SSL error in Android Studio's run log.

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/");
}

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

the error message I receive in Android studio's is:

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

"E/chromium: [ERROR:ssl_client_socket_impl.cc(947)] handshake failed; returned -1, SSL error code 1, net_error -202"

推荐答案

尽管有以下ssl错误,您仍需要扩展WebViewClient以允许加载.

You need to extend the WebViewClient to allow loading despite ssl errors as follows.

    // 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
    }
}

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

The site has issues with ssl certificate verification. The default WebViewClient stops loading when there is an ssl verification error. This extension will instruct it to disregard the error and proceed.

因此更改行webview.setWebViewClient(new WebViewClient());

webview.setWebViewClient(new IgnoreSSLErrortWebViewClient());

这现在应该对您有用.

这篇关于尝试在WebView中加载URL时出错-Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆