Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if webview failed to load page (android)?

Tags:

android

I have a webview in my app, however sometimes due to connectivity the webview fails to load and I get the default webpage unavailable page. I want to show an alertdialog if the webview failed to load. Is there anyway I can check (maybe in the shouldOverridePageLoad function) that a webview loaded successfully? Thanks again

like image 573
HAxxor Avatar asked Oct 22 '12 17:10

HAxxor


People also ask

Why is my WebView not working?

You might often face issues in updating the chrome and Android System Webview. To fix this problem, you can reboot your device, check your internet connection, stop auto-updating all apps, clear Google Playstore cache, and storage, leave the beta testing program, and manually update Android WebView app from Playstore.

How do I load WebView?

Modify src/MainActivity. java file to add WebView code. Run the application and choose a running android device and install the application on it and verify the results. Following is the content of the modified main activity file src/MainActivity.

How does WebView work in Android?

The WebView class is an extension of Android's View class that allows you to display web pages as a part of your activity layout. It does not include any features of a fully developed web browser, such as navigation controls or an address bar. All that WebView does, by default, is show a web page.


Video Answer


2 Answers

Use a WebClient on your web view as follow :

webView.setWebViewClient(new WebViewClient(){  @Override public void onReceivedError(WebView view, WebResourceRequest request,       WebResourceError error) {     super.onReceivedError(view, request, error);     // Do something   } }); 
like image 135
Damien Praca Avatar answered Oct 03 '22 09:10

Damien Praca


Extending on Damien's answer on using WebViewClient, there are four listeners available on WebViewClient to check for the success and failure of loading web pages.

webView.setWebViewClient(new WebViewClient() {     @Override     public void onPageFinished(WebView view, String url) {     }      @Override     public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {     }      @Override     public void onReceivedHttpError(             WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {     }      @Override     public void onReceivedSslError(WebView view, SslErrorHandler handler,                                    SslError error) {     } }); 

There is also:

@Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {    } }); 

which is deprecated in favor of its overload mentioned in the above code.

like image 42
Klaus Avatar answered Oct 03 '22 09:10

Klaus