Android WebView can not load HTTPS

When developing some Android apps, I have a problem with WebView. Some HTTPS urls aren't working, it say "This page can not displayed", but they are working well in browser. The problem is Certificate of SSL, when your WebView can't authorize the website. So, I have a simple solution to ignore this error: catch the event "onReceivedSslError" of WebViewClient and set WebViewClient object in WebView:

public void onCreate(Bundle savedInstance)
{       
    super.onCreate(savedInstance);
    setContentView(R.layout.your_layout);

    webView=(WebView)findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new DialogWebViewClient());
    String url ="https://yoursite.com";
    webView.loadUrl(url);
    
}

private class DialogWebViewClient extends WebViewClient {
    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        handler.proceed();
    }
}

Leave a Reply

Powered by Blogger.