Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error while trying to load a URL in webview - android

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

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.

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

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

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

like image 315
ishi Prinsloo Avatar asked Jul 14 '19 12:07

ishi Prinsloo


Video Answer


1 Answers

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

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.

So change the line webview.setWebViewClient(new WebViewClient());

to

webview.setWebViewClient(new IgnoreSSLErrortWebViewClient());

this should work fine for you now.

like image 98
OGB Avatar answered Sep 21 '22 01:09

OGB