defiant
defiant

Reputation: 3341

OnRecivedError doesn't show the custom error page

I am using onReceivedError with a custom error page to show when internet is not available in a WebView. Below is the code I use for it. It doesn't work. It simply shows the webpage not available page when internet is not available.

Anyway the logcat shows me this error:

I/chromium﹕ [INFO:CONSOLE(0)] "Not allowed to load local resource: file:///android_asset/webkit/android-weberror.png", source: data:text/html,chromewebdata (0)

my code is

private class myWebViewBrowser extends WebViewClient {
    /*@Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }*/
    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        super.onReceivedError(view, request, error);
        Log.e(String.valueOf(error.getErrorCode()), error.getDescription().toString());
        view.loadUrl("file:///android_asset/error.html");
    }
}

Upvotes: 4

Views: 1389

Answers (3)

RBK
RBK

Reputation: 2417

you can change

view.loadUrl(...);

to

view.loadDataWithBaseURL( "file:///android_asset/", html, "text/html","utf-8", null );

where

  • "file:///android_asset/" is your base url for load html

  • html is your html string

  • "text/html" is mime type for content

  • "utf-8" is encoding style

  • and last url as null, its for history reference.

Upvotes: 4

Amaresh Jana
Amaresh Jana

Reputation: 742

    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        Log.e(String.valueOf(error.getErrorCode()), error.getDescription().toString());
        view.loadDataWithBaseURL( "file:///android_asset/", html, "text/html","utf-8", null );   
        }

Upvotes: 1

Prabhakar
Prabhakar

Reputation: 523

This might be helpful :

https://groups.google.com/forum/#!topic/android-developers/4g6H0vr5_0E

Try Following Code :

private class myWebViewBrowser extends WebViewClient {

@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {

    Log.e(String.valueOf(error.getErrorCode()), error.getDescription().toString());
    view.loadDataWithBaseURL( "file:///android_asset/", html, "text/html","utf-8", null );   
    }
}

Upvotes: 1

Related Questions