5

I need to display a website in a WebView. I went through almost all possible solution but my WebView doesn't display the site correctly. Following is my code:

progDailog = ProgressDialog.show(this, "Loading","Please wait...", true);
        progDailog.setCancelable(false);

        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient(){

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                progDailog.show();
                view.loadUrl(url);

                return true;
            }
            @Override
            public void onPageFinished(WebView view, final String url) {
                progDailog.dismiss();
            }
        });

        webView.loadUrl("https://www.site.ru/login");

If I copy the URL 'https://www.site.ru/login' in a mobile browser it displays correctly but in Android app when I tried to load same URL in WebView, but it shows progressBar then everything is displayed white and blank in WebView Screen.

UPD: my xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:id="@+id/sign_up_web_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></WebView>


</RelativeLayout>
g71132
  • 671
  • 2
  • 10
  • 17

3 Answers3

2

First, create a class that extends WebViewClient and which is set to ignore SSL errors:

// SSL Error Tolerant Web View Client
private class SSLWebViewClient extends WebViewClient {

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed(); // Ignore SSL certificate errors
            }

}

Then with your web view object, set its web view client to be an instance of the override class:

 webView.setWebViewClient(
                new SSLWebViewClient();
        );

check this thread

KDeogharkar
  • 10,939
  • 7
  • 51
  • 95
  • thank, i add `@Override public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { handler.proceed(); // Ignore SSL certificate errors }` but i have still same problem. And i add `@Override public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error){ Toast.makeText(SignUpActivity.this, "Your Internet Connection May not be active Or " + error , Toast.LENGTH_LONG).show(); }` for check my error, but i have not error – g71132 Aug 12 '16 at 10:51
0

If Logcat is showing errors similar to:

I/chromium: [INFO:CONSOLE(1)] "TypeError: Cannot read property 'getItem' of null"
I/chromium: [INFO:CONSOLE(9)] "Uncaught TypeError: Cannot read property 'getItem'

Enable Dom Storage by adding the following:

webView.settings.setDomStorageEnabled(true);

This seems to allow the browser to store a DOM model of the page elements, so that Javascript can perform operations on it.

RobertoAllende
  • 8,744
  • 4
  • 30
  • 49
0

Mostly it is because unsecure connections or SSL errors, so you need to implement a class that extends SSLWebViewClient class and override onReceivedSslError method and ignore ssl errors likse this :

class SSLWebViewClient : WebViewClient() {

    override fun onReceivedSslError(view: WebView?, handler: SslErrorHandler?, error: SslError?) {
        handler?.proceed()
    }
}

then :

in onCreate

onCreate() {
    ...
    webView = findViewById(R.id.webView)
    webView.webViewClient = SSLWebViewClient()
    webView.settings.javaScriptEnabled = true
    webView.settings.domStorageEnabled = true
    webView.settings.builtInZoomControls = true
   ...
}
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Megz
  • 1