Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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");
    }
}
like image 672
defiant Avatar asked Sep 10 '15 03:09

defiant


3 Answers

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.

like image 84
RBK Avatar answered Nov 09 '22 00:11

RBK


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 );   
    }
}
like image 28
Prabhakar Avatar answered Nov 08 '22 23:11

Prabhakar


    @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 );   
        }
like image 38
Amaresh Jana Avatar answered Nov 08 '22 22:11

Amaresh Jana