1

When WebView is loading url, user can lose access to internet (for example, he work with router, but money in account is ended).

In this case WebView show own html "Webpage not available".

I try to use custom WebViewClient and callback:

@Override
    public void onReceivedError(android.webkit.WebView view, WebResourceRequest request, WebResourceError error) {
        super.onReceivedError(view, request, error);
    }

but it`s not called.

How to handle offline when loading url in WebView?

P.S. Do not offer solutions to check the network status, please. I am interested only WebView.

Atetc
  • 1,643
  • 19
  • 26
  • I found the reason why not work error callbacks of my WebView: getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); – Atetc Oct 28 '15 at 09:16

4 Answers4

0

Try this :

 @Override
  public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
    Log.e(String.valueOf(errorCode), description);

    // API level 5: WebViewClient.ERROR_HOST_LOOKUP
    if (errorCode == -2) {
      String summary = "<html><body style='background: black;'><p style='color: red;'>Unable to load information. Please check if your network connection is working properly or try again later.</p></body></html>";       view.loadData(summary, "text/html", null);
      return;
    }

    // Default behaviour
    super.onReceivedError(view, errorCode, description, failingUrl);
  }
Jas
  • 3,207
  • 2
  • 15
  • 45
  • First, this method is depricated. Second, this method not called if device in offline, but "onPageFinished" called for me. – Atetc Oct 28 '15 at 07:18
  • I found the reason why not work error callbacks of my WebView: getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); – Atetc Oct 28 '15 at 09:15
0

Hey do this for give popup to user that check your data.. StackOverflow link

AndroidHive link

Community
  • 1
  • 1
Sumit Pathak
  • 529
  • 2
  • 7
  • 24
0

I found the reason why not work error callbacks of my WebView:

getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);

After removing, this method:

@Override
    public void onReceivedError(android.webkit.WebView view, int errorCode, String description, String failingUrl) {
        super.onReceivedError(view, errorCode, description, failingUrl);
    }

started working again

Atetc
  • 1,643
  • 19
  • 26
-1

1.Save yout error template as error.html in asset

2.when error occurs load template from asset webview.loadUrl("file:///android_asset/error.html");

Akash kv
  • 429
  • 4
  • 13