NullPointerException
NullPointerException

Reputation: 37731

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");
    }

   });

Upvotes: 7

Views: 5248

Answers (1)

Frost
Frost

Reputation: 883

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");
}

Upvotes: 14

Related Questions