Basic
steps to show WebView in the application
Use
WebView element in the layout xml.
Provide
internet permission in the AndroidManifest.xml file.
loadUrl
on WebView object.
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>
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>
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.