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:layout_width="fill_parent"  
      android:layout_height="fill_parent"  
      android:orientation="vertical" >  
      <WebView  
           android:id="@+id/ displayWebview "  
           android:layout_width="fill_parent"  
           android:layout_height="fill_parent" />  
 </LinearLayout>   
  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" />  
     ...  
 </manifest>  
  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());
webView.loadUrl("https://www.google.co.in/");

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”

Issue:
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”.

Solution:

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() {
@Override
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.";
            break;
        case SslError.SSL_EXPIRED:
            message = "The certificate has expired.";
            break;
        case SslError.SSL_IDMISMATCH:
            message = "The certificate Hostname mismatch.";
            break;
        case SslError.SSL_NOTYETVALID:
            message = "The certificate is not yet valid.";
            break;
    }
    message += " Do you want to continue anyway?";
    alertDialog.setTitle("SSL Certificate Error");
    alertDialog.setMessage(message);
    alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Log.d("CHECK", "Button ok pressed");
            // Ignore SSL certificate errors
            handler.proceed();
        }
    });
    alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Log.d("CHECK", "Button cancel pressed");
            handler.cancel();
        }
    });
    alertDialog.show();
    }
});
webView.loadUrl("https://www.google.co.in/");




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.

6 comments :

  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.

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

    ReplyDelete
  3. 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!

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

    ReplyDelete
  5. Thanks.This was very helpfull for me.I was tryingn from last three hours.Now i got solution from here.

    ReplyDelete