Sunday, April 8, 2012

Prevent WebView from displaying "web page not available”


I have an app that makes extensive use of a WebView. When the user of this app does not have Internet connection, a page saying "web page not available" and various other text appears. Is there a way to not show this generic text in my WebView? I would like to provide my own error handling.




private final Activity activity = this;

private class MyWebViewClient extends WebViewClient
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
// I need to do something like this:
activity.webView.wipeOutThePage();
activity.myCustomErrorHandling();
Toast.makeText(activity, description, Toast.LENGTH_LONG).show();
}
}



I found out WebView->clearView doesn't actually clear the view.


Source: Tips4all

7 comments:

  1. First create your own error page in HTML and put it in your assets folder, Let's call it myerrorpage.html
    Then with onReceivedError:

    mWebView.setWebViewClient(new WebViewClient() {
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
    mWebView.loadUrl("file:///android_asset/myerrorpage.html");

    }
    });

    ReplyDelete
  2. Check out the discussion at Android WebView onReceivedError(). It's quite long, but the consensus seems to be that a) you can't stop the "web page not available" page appearing, but b) you could always load an empty page after you get an onReceivedError

    ReplyDelete
  3. Perhaps I misunderstand the question, but it sounds like you're saying you get the error received callback, and you just are asking what is the best way to not show the error? Why don't you just either remove the web view from the screen and/or show another view on top of it?

    ReplyDelete
  4. I suppose that if you insist on doing this, you could just check if the resource is there before calling the loadURL function.
    Just simply override the functions and do the check before calling the super()

    REMARK (maybe off-topic): In http, there is a method called HEAD which is described as follow:


    The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response


    This method might be handy.
    Anyway how ever you implement it ... check this code:

    import java.util.Map;

    import android.content.Context;
    import android.webkit.WebView;

    public class WebViewPreLoad extends WebView{

    public WebViewPreLoad(Context context) {
    super(context);
    }
    public void loadUrl(String str){
    if(//Check if exists)
    super.loadUrl(str);
    else
    //handle error
    }
    public void loadUrl(String url, Map<String,String> extraHeaders){
    if(//Check if exists)
    super.loadUrl(url, extraHeaders);
    else
    //handle error
    }
    }


    You could try this check using

    if(url.openConnection().getContentLength() > 0)

    ReplyDelete
  5. You could use a GET request to get the page content and then display that data using the Webview , this way you are not using multiple server calls. Alternative you can use Javascript to check the DOM object for validity.

    ReplyDelete
  6. try this shouldOverrideUrlLoading , before redirect to another url check for internet conncetion based on that page should be loaded or not.

    ReplyDelete
  7. I would just change the webpage to whatever you are using for error handling:

    getWindow().requestFeature(Window.FEATURE_PROGRESS);
    webview.getSettings().setJavaScriptEnabled(true);
    final Activity activity = this;
    webview.setWebChromeClient(new WebChromeClient() {
    public void onProgressChanged(WebView view, int progress) {
    // Activities and WebViews measure progress with different scales.
    // The progress meter will automatically disappear when we reach 100%
    activity.setProgress(progress * 1000);
    }
    });
    webview.setWebViewClient(new WebViewClient() {
    public void onReceivedError(WebView view, int errorCode, String description, String
    failingUrl) {
    Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
    }
    });
    webview.loadUrl("http://slashdot.org/");


    this can all be found on http://developer.android.com/reference/android/webkit/WebView.html

    ReplyDelete