Reputation: 691
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>
Upvotes: 5
Views: 4191
Reputation: 1
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
...
}
Upvotes: 0
Reputation: 9508
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.
Upvotes: 0
Reputation: 10959
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
Upvotes: 2