Android WebViewClient 没有返回正确的错误
Android WebViewClient not returning proper error
我的代码中有一个 WebView
,它使用自定义 WebViewClient
。我在 CustomWebViewClient
文件中覆盖了各种错误方法。
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) {
}
}
现在,我想在我的 WebView 中处理各种类型的错误。我尝试了以下场景。
1) 加载一个不存在的本地html"file:///android_asset/main.html"。
-- 在这种情况下,我正在回调第一种方法,即 OnReceivedError()
。
2) 加载托管在网络上的 html 页面,即 http://help.websiteos.com/websiteos/example_of_a_simple_html_page.ht。这个html也不存在。但在这种情况下,我没有得到任何对错误方法的回调。
检查 Chrome 时,出现以下错误
GET http://help.websiteos.com/websiteos/example_of_a_simple_html_page.ht 404 (Not Found),很明确的说明资源不可用。那为什么 WebView 没有返回任何错误回调?
在这两种情况下,我请求的资源不可用。但我只在第一种情况下收到错误回调。
您必须在 api 小于 23 的设备上安装 运行。
url http://help.websiteos.com/websiteos/example_of_a_simple_html_page.ht
returns 回调到 onReceiveHttpError()。但是这个方法是从API level 23才添加的。
Reference
我的代码中有一个 WebView
,它使用自定义 WebViewClient
。我在 CustomWebViewClient
文件中覆盖了各种错误方法。
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) {
}
}
现在,我想在我的 WebView 中处理各种类型的错误。我尝试了以下场景。
1) 加载一个不存在的本地html"file:///android_asset/main.html"。
-- 在这种情况下,我正在回调第一种方法,即 OnReceivedError()
。
2) 加载托管在网络上的 html 页面,即 http://help.websiteos.com/websiteos/example_of_a_simple_html_page.ht。这个html也不存在。但在这种情况下,我没有得到任何对错误方法的回调。
检查 Chrome 时,出现以下错误
GET http://help.websiteos.com/websiteos/example_of_a_simple_html_page.ht 404 (Not Found),很明确的说明资源不可用。那为什么 WebView 没有返回任何错误回调?
在这两种情况下,我请求的资源不可用。但我只在第一种情况下收到错误回调。
您必须在 api 小于 23 的设备上安装 运行。 url http://help.websiteos.com/websiteos/example_of_a_simple_html_page.ht returns 回调到 onReceiveHttpError()。但是这个方法是从API level 23才添加的。 Reference