Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebView onPageFinished is being called when Page Not Available error. ¿Solution?

I have a webview that must make a LOG when onReceivedError() is called. But the problem is that when i have very bad wifi connection (almost 100% loss) and the webview is showing a Page Not Available html error, the method onPageFinished is being called and onReceivedError is not being called

How can this be solved? I want that when the page is not available the method onReceivedError gets called!

    webView.setWebViewClient(new WebViewClient() {   

    @Override
    public void onPageFinished(WebView view, String url) {
     super.onPageFinished(view, url);
     Log.d("WEBVIEw","ON PAGE FINISHED");
    }    

    @Override
    public void onReceivedError(WebView view, int errorCode,
      String description, String failingUrl) {
      Log.d("WEBVIEw","ON PAGE error");
    }

   });
like image 388
NullPointerException Avatar asked May 21 '14 10:05

NullPointerException


1 Answers

onPageFinished will always be triggered even if there is an error. It would be great to have a method called onPageSucceeded - but this is fairly easy to create.

Create a property on the top of the page private boolean webviewSuccess = true; and

@Override
public void onPageFinished(WebView view, String url) {
 super.onPageFinished(view, url);
 Log.d("WEBVIEW","ON PAGE FINISHED");
 if(webviewSuccess) {
    Log.d("WEBVIEW","ON PAGE SUCCEEDED");
 }
}    

@Override
public void onReceivedError(WebView view, int errorCode,
  String description, String failingUrl) {
  webviewSuccess = false;
  Log.d("WEBVIEW","ON PAGE error");
}
like image 183
Frost Avatar answered Sep 21 '22 17:09

Frost