Reputation: 7348
I have a WebView
in my code which uses a custom WebViewClient
. I have overridden various error methods in my CustomWebViewClient
file.
public class CustomWebViewClient extends WebViewClient {
@SuppressWarnings("deprecation")
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
}
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onReceivedHttpError(
WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
}
}
Now, I want to handle various types of errors in my WebView. I tried with the following scenarios.
1) Load a local html "file:///android_asset/main.html" which does not exist.
-- In this scenario, I am getting callback to the first method i.e. OnReceivedError()
.
2) Load a html page hosted on the web i.e. http://help.websiteos.com/websiteos/example_of_a_simple_html_page.ht. This html also does not exist. But in this case I am not getting any callback to the error method.
While inspecting on the Chrome, I am getting the following error
GET http://help.websiteos.com/websiteos/example_of_a_simple_html_page.ht 404 (Not Found), which clearly shows that the resource is not available. Then why is the WebView not returning any callback for errors ?
In both the cases, I am requesting a resource which is not available. But I am getting error callbacks only in the first scenario.
Upvotes: 1
Views: 441
Reputation: 76
You must have run on a device whose api is < 23. The url http://help.websiteos.com/websiteos/example_of_a_simple_html_page.ht returns callback to onReceiveHttpError(). But this method is only added from API level 23. Reference
Upvotes: 1