[英]Android WebView onReceivedError()
Does anyone know if there is a way to intercept a "page not found" or "page not loading error" in WebView? 有没有人知道在WebView中是否有办法拦截“找不到页面”或“页面未加载错误”?
According to the android documentation, onReceivedError()
should be able to intercept. 根据android文档,
onReceivedError()
应该能够拦截。 but i tested it in an app which I deleberately gave the wrong URL, and it didn't do anything. 但我在一个应用程序测试它,我删除了错误的URL,它没有做任何事情。
I want my app to be able to give my own custom error message if the URL is ever unavailable for any reason. 我希望我的应用程序能够提供我自己的自定义错误消息,如果URL由于任何原因不可用。
this is the code that did nothing: 这是没有做任何事情的代码:
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
// custom error handling ... show and alert or toast or something
}
According to documentation and my experience it should work quite fine. 根据文档和我的经验,它应该工作得很好。 You just have to set your
WebClient
with overriden method onReceivedError
in your WebView. 您只需在WebView中使用覆盖方法
onReceivedError
设置WebClient
。
Here is the snippet from some of my old test app: 这是我的一些旧测试应用程序的片段:
WebView wv = (WebView) findViewById(R.id.webView);
wv.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.i("WEB_VIEW_TEST", "error code:" + errorCode);
super.onReceivedError(view, errorCode, description, failingUrl);
}
});
I've tested it and it works quite fine. 我测试了它,它的工作原理非常好。 Check your logs and see what kind of code error do you get.
检查您的日志,看看你得到了什么样的代码错误。 Hope it helps.
希望能帮助到你。
I have tried using onReceivedError both inside shouldOverrideUrlLoading() and outside that method but in the WebViewClient. 我已尝试在shouldOverrideUrlLoading()内部和该方法之外使用onReceivedError但在WebViewClient中。 I even tried outside in the main Activity class.
我甚至在主要的Activity类中尝试过。 I was not happy with the inconsistent results.
我对结果不一致感到不满意。 So I settled on using a test method, isOnline(), and calling that before calling loadUrl().
所以我决定使用测试方法isOnline(),并在调用loadUrl()之前调用它。
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getBaseContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo i = cm.getActiveNetworkInfo();
if ((i == null) || (!i.isConnected())) {
Toast toast = Toast.makeText(getBaseContext(),
"Error: No connection to Internet", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP | Gravity.CENTER, 0, 0);
toast.show();
return false;
}
return true;
}
Then this onReceivedError is in the WebViewClient but outside the overloadurlthingy method. 然后,这个onReceivedError位于WebViewClient中,但在overloadurlthingy方法之外。 This seems to consistently prevent the stupid, smirking-android error pages.
这似乎始终防止愚蠢,傻笑的android错误页面。
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
if (view.canGoBack()) {
view.goBack();
}
Toast toast = Toast.makeText(getBaseContext(), description,
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP | Gravity.CENTER, 0, 0);
toast.show();
}
Some people might consider this resource-heavy. 有些人可能会认为这种资源很重。 Well, not heavy the way the Android Facebook and Google+ apps are.
嗯,不像Android Facebook和Google+应用程序那么重。 And not the way Google services are.
而不是谷歌服务的方式。 I frankly don't mind using up a little of those apps oxygen.
坦率地说,我不介意使用那些应用程序氧气。 Call me a bad boy...
叫我一个坏男孩......
You should use this after the on Page finished 你应该在页面完成后使用它
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error){
//Your code to do
Toast.makeText(getActivity(), "Your Internet Connection May not be active Or " + error , Toast.LENGTH_LONG).show();
}
Remember use both onReceivedError methods because the method with the description parameter is deprecated. 请记住同时使用onReceivedError方法,因为不推荐使用带有description参数的方法。 We use this deprecated method for below API 23 support.
我们将此弃用方法用于以下API 23支持。 Hence we can use it in all SDK versions.
因此,我们可以在所有SDK版本中使用它。
Here how I do it - 我是怎么做的 -
webview.setWebViewClient(new WebViewClient() {
@SuppressWarnings("deprecation")
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
try {
webview.stopLoading();
} catch (Exception e) {
e.printStackTrace();
}
if (webview.canGoBack()) {
webview.goBack();
}
showkError();
}
@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
try {
webview.stopLoading();
} catch (Exception e) {
e.printStackTrace();
}
if (webview.canGoBack()) {
webview.goBack();
}
showError();
}
@Override
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
super.onReceivedHttpError(view, request, errorResponse);
try {
webview.stopLoading();
} catch (Exception e) {
e.printStackTrace();
}
if (webview.canGoBack()) {
webview.goBack();
}
showError();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.