58

In the Android SDK 23 onReceivedError(WebView view, int errorCode, String description, String failingUrl) has been deprecated and replaced with onReceivedError(WebView view, WebResourceRequest request, WebResourceError error). However if I put my phone in Airplane mode and load an url on my WebView, only the deprecated version of the method is called.

onReceivedHttpError (WebView view, WebResourceRequest request, WebResourceResponse errorResponse) is also not useful, as it only detects errors higher than 500, and I am getting a 109 status code.

Is there a non-deprecated way of detecting that my WebView failed to load?

7
  • make sure you are testing with Android SDK 23
    – karan
    Commented Sep 25, 2015 at 11:36
  • @KaranMer, that is already the case. Commented Sep 25, 2015 at 14:26
  • 4
    Does the mobile device where you are testing actually run Android Marshmallow (API 23)? Even if you develop your app on API 23 SDK, but then run the app on Android Lollipop, you will still be getting the "old" onReceivedError, because it's the feature of the OS, not of an SDK. Also, the "error code 109" (I guess, this is net::ERR_ADDRESS_UNREACHABLE) is not an HTTP error code, it's Chrome's error code. onReceivedHttpError is only called for the errors received from the server via HTTP. When the device is in airplane mode, it can't possibly receive a reply from a server. Commented Sep 26, 2015 at 1:02
  • @MikhailNaganov that is helpful. if you post that as an answer, I will accept it. Commented Sep 26, 2015 at 20:36
  • So the solution is to override both methods in the client, and expect the OS to determine which one is called, I guess. Commented Aug 31, 2016 at 13:23

2 Answers 2

140

You could also do following:

@SuppressWarnings("deprecation")
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
    // Handle the error
}

@TargetApi(android.os.Build.VERSION_CODES.M)
@Override
public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr) {
    // Redirect to deprecated method, so you can use it in all SDK versions
    onReceivedError(view, rerr.getErrorCode(), rerr.getDescription().toString(), req.getUrl().toString());
}

Make sure you import android.annotation.TargetApi

19
  • 2
    this works! 30.11.2015 , on android 4x, with latest sdk for 5x
    – jmp
    Commented Nov 30, 2015 at 19:16
  • 13
    Please be aware that the new SDK 23 callback will be called for any resource (iframe, image, etc) that failed to load, not just for the main page... therefore your handling of errors may need to be modified accordingly.
    – k2col
    Commented Apr 19, 2016 at 6:30
  • 4
    @ShahidSarwar That's wrong! The problem you had is in no way related to the code discussed in this question. The onReceivedSslError callback and what you did there is an entirely different subject.
    – caw
    Commented Feb 20, 2017 at 15:30
  • 3
    @ShahidSarwar tried publishing and it got published, now what? Commented Jul 5, 2017 at 12:24
  • 3
    @ShahidSarwar : Sorry, the link you shared here is a different topic, and it is not having any relevance in this context. The person who asked the question there used 'handler.proceed()'; blindly on the 'onReceivedSslError' callback. That's why Google rejected it, and I also met the same situation once. Anyway, the answer here by xdevs23 is the best, at-least for the time being.
    – Midhu
    Commented Aug 4, 2017 at 4:16
26

Please note that the mobile device where you are testing needs to actually run Android Marshmallow (API 23). Even if you develop your app on API 23 SDK, but then run the app on Android Lollipop, you will still be getting the "old" onReceivedError, because it's the feature of the OS, not of an SDK.

Also, the "error code 109" (I guess, this is net::ERR_ADDRESS_UNREACHABLE) is not an HTTP error code, it's Chrome's error code. onReceivedHttpError is only called for the errors received from the server via HTTP. When the device is in airplane mode, it can't possibly receive a reply from a server.

2
  • 10
    how can this be the answer.. it doesn't give the solution
    – Kasnady
    Commented Sep 2, 2017 at 8:46
  • 1
    The solution is use a device running >=23
    – MDjava
    Commented Nov 8, 2018 at 5:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.