Tuesday, July 1, 2014

Android : Workaround for webview not loading https url

Basic steps to show WebView in the application
  1. Use WebView element in the layout xml.
  2. Provide internet permission in the AndroidManifest.xml file.
  3. loadUrl on WebView object.
  1. main.xml in the res/layout folder

 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
      android:orientation="vertical" >  
           android:id="@+id/ displayWebview "  
           android:layout_height="fill_parent" />  
  1. Application must have access to the Internet. To get Internet access, request the INTERNET permission in the AndroidManifest.xml file

 <manifest ... >  
   <uses-permission android:name="android.permission.INTERNET" />  
  1. call the loadUrl() on webview object inside the onCreate() method of your activity class and pass the url as a parameter to it .

WebView webView = (WebView) findViewById(R.id.displayWebview);
webView.setWebViewClient(new WebViewClient());

Problem: WebView is not loading if the request is https.
When we try url with https request, we may face blank page or error message saying “web page not available”

WebView support ssl default and If we want to support third party then with few methods like setCertificate and so on we can make it. However, WebView Shows blank screen if it is not supporting the certificates. So, Catch the exception in onReceivedSslError method and do respective action when certificate is not supported. When certificate is not supported we may get blank screen or “Web Page not available, The webpage at might be temporarily down or it may have moved permanently to a new web address”.


There is a Work around to solve the issue by Overriding onReceivedSslError method of WebViewClient as shown below.

webView = (WebView) findViewById(R.id.displayWebview);
webView.setWebViewClient(new WebViewClient() {
public void onReceivedSslError(final WebView view, final SslErrorHandler handler, SslError error) {
    Log.d("CHECK", "onReceivedSslError");
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    AlertDialog alertDialog = builder.create();
    String message = "Certificate error.";
    switch (error.getPrimaryError()) {
        case SslError.SSL_UNTRUSTED:
            message = "The certificate authority is not trusted.";
        case SslError.SSL_EXPIRED:
            message = "The certificate has expired.";
        case SslError.SSL_IDMISMATCH:
            message = "The certificate Hostname mismatch.";
        case SslError.SSL_NOTYETVALID:
            message = "The certificate is not yet valid.";
    message += " Do you want to continue anyway?";
    alertDialog.setTitle("SSL Certificate Error");
    alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Log.d("CHECK", "Button ok pressed");
            // Ignore SSL certificate errors
    alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Log.d("CHECK", "Button cancel pressed");

Thanks for reading :) 
Whether this post is helpful?

Have something to add to this post? If you have any other quick thoughts/hints that you think people will find useful? comments / feedback are welcome.


  1. Why is every single site or Stackoverflow subjects is demonstrating how to handle.proceed() or handle.cancel() ??? Is there no method to just trust our certificate by adding it in a keystore or something ? I mean, my certificate is signed by a Trusted CA Provider, recognized in green label in browser. But the site is not displayed into webview.

  2. Its not the certificate of the app, certificate of the URL hitting.

  3. I am getting error in webview "preview not available", if I am using url which is having space in url like https://s3-ap-southeast-1.amazonaws.com/temp folder/Test file.html. How can I load this type url in android webview. while this type url is working in browser.

  4. I am loading multiple amazon affiliate images and its causing multiple SSL error and alert dialogs in my webview.
    This will annoy users to always click on positive "OK" button!
    Please help!

  5. Wow!
    It was a life saver for me.
    Thanks a lot