如何处理来自 webView 的错误?
我有这样的代码:
WebChromeClient webViewChromeClient = new WebChromeClient() {
public void onProgressChanged(WebView view, int progress){
}
};
webView.setWebChromeClient(webViewChromeClient);
WebViewClient webViewClient = new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
super.onReceivedError(view, errorCode, description, failingUrl);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
};
我想以在视图中不显示任何内容的方式处理错误(404 或连接中断时的错误)。
怎么做呢?
I have this code:
WebChromeClient webViewChromeClient = new WebChromeClient() {
public void onProgressChanged(WebView view, int progress){
}
};
webView.setWebChromeClient(webViewChromeClient);
WebViewClient webViewClient = new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
super.onReceivedError(view, errorCode, description, failingUrl);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
};
And I want to handle errors (404 or error if connection broken) in such a way that in the view showed nothing.
How to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
WebView频繁调用应用程序回调(通过WebViewClient和WebChromeClient),
从 onReceivedError 实现函数来捕获并定义抛出的异常
然后使用下面的 showError 方法你可以了解发生了什么样的异常。
在
res/layout
文件夹中创建自定义视图,将其命名为ic_dialog_alert
添加标题和错误消息的文本视图另外,Webview 会因资源呈现问题而终止,该问题可以通过 终止处理 API
WebView frequently calls application callbacks (via WebViewClient and WebChromeClient),
From onReceivedError implement the function to catch and define the Thrown Exeption
Then using the following showError method you can understand what kind of exception has occured.
Create your custom view in
res/layout
folder name it toic_dialog_alert
add textviews for title and error messageAlso the Webview be terminated by Resource rendering problem which can be handle by Termination Handling API