我将 onReceivedError
与自定义错误页面一起使用,以在 WebView
中显示互联网何时不可用。下面是我使用的代码。它不起作用。当互联网不可用时,它只是显示网页不可用页面。
无论如何,logcat 向我显示了这个错误:
I/chromium﹕ [INFO:CONSOLE(0)] "Not allowed to load local resource: file:///android_asset/webkit/android-weberror.png", source: data:text/html,chromewebdata (0)
我的代码是
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");
}
}
最佳答案
你可以改变
view.loadUrl(...);
到
view.loadDataWithBaseURL( "file:///android_asset/", html, "text/html","utf-8", null );
在哪里
“file:///android_asset/”是您加载 html 的基本 url
html 是你的 html 字符串
"text/html"是内容的 mime 类型
"utf-8"是编码风格
最后一个 url 为 null,供历史引用。
关于android - OnRecivedError 不显示自定义错误页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32492570/