问题描述
在的WebView的Android我试图加载一个网址,并以检查此URL的负载成功完成(互联网连接可用,在服务器启动等),我是IM pression的web视图下.loadUrl会抛出异常,但错了!因为它明确地在表示:一个异常不会被抛出 。
In webview android I am trying to load a url and in order to check if the load of this url is done successfully (internet connection was available, the server was up etc) I was under the impression that webview.loadUrl would throw exceptions, but wrong! as it explicitly is stated in here "an exception will NOT be thrown".
所以,我该怎么检查,看看是否webview.loadUrl没有失败?
So how can I check to see if webview.loadUrl did not fail ?
推荐答案
不幸的是,目前没有在WebView中没有简单的方法,以确保在页面上,一切都已经被成功加载。我们希望一个更好的API来在未来的版本中。让我解释一下你现在可以做什么。
Unfortunately, currently there is no easy way in WebView to ensure that everything on the page has been loaded successfully. We are hoping for a better API to come up in future version. Let me explain what you can do now.
首先,为了检测美元,向服务器的连接,用于加载你的主页(如不良域名,I / O错误等)p $ pvent的WebView任何问题,你应该使用 WebViewClient.onReceivedError
回调为其他人的正确建议:
First of all, in order to detect any problems that prevent WebView from making a connection to the server for loading your main page (e.g. bad domain name, I/O error, etc.), you should use WebViewClient.onReceivedError
callback as other people correctly suggest:
public class MyWebViewClient extends WebViewClient {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
// Make a note about the failed load.
}
}
myWebView.setWebViewClient(new MyWebViewClient());
如果服务器连接成功,主要网页被检索和分析,你会收到 WebView.onPageFinished
的回调,所以你也需要有这样在你的 WebViewClient
子类:
If the server connection was successful, and the main page was retrieved and parsed, you will receive WebView.onPageFinished
callback, so you also need to have this in your WebViewClient
subclass:
public class MyWebViewClient extends WebViewClient {
...
@Override
public void onPageFinished(WebView view, String url) {
// Make a note that the page has finished loading.
}
...
}
这里需要说明的是,如果你收到一个HTTP错误从服务器(如404或500错误),此回调无论如何都会被调用,它只是你会在你的WebView获得内容将是一个服务器错误页面。人建议如何处理它,看到这里的答案不同的方式:How我可以在Android的WebView检查,如果一个页面是"未发现404页和QUOT;?基本上,它真的取决于你所期望的是一个好页和错误的页面。不幸的是,目前还没有办法让该应用从的WebView获得HTTP响应code。
The caveat here is that if you have received an HTTP error from the server (e.g. a 404 or a 500 error), this callback will be called anyway, it's just the content that you will get in your WebView will be a server error page. People suggest different ways of how to deal with it, see the answers here: How can I check from Android WebView if a page is a "404 page not found"? Basically, it really depends on what you expect to be a "good" page and a "error" page. Unfortunately, there is currently no way for the app to get the HTTP response code from WebView.
该回调 WebViewClient.onPageStarted
和 WebViewClient.onProgressChanged
只有当你想画一个进度条有用您正在加载的页面。
The callbacks WebViewClient.onPageStarted
and WebViewClient.onProgressChanged
are only useful if you want to draw a progress bar as you are loading the page.
另外请注意,覆盖的方法 WebViewClient.shouldOverrideUrlLoading
人们通常认为是不正确的:
Also note that the way of overriding WebViewClient.shouldOverrideUrlLoading
that people usually suggest is not correct:
public class MyWebViewClient extends WebViewClient {
...
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// !!! DO NOT DO THIS UNCONDITIONALLY !!!
view.loadUrl(url);
return true;
}
...
}
什么少数开发商意识到的是,回调也被称为与非HTTPS模式的子帧。如果你会遇到类似< IFRAME SRC ='电话:1234'>
,你最终会执行 view.loadUrl('电话:1234')
和您的应用程序将显示一个错误页面,因为的WebView不知道如何加载电话:
URL。
建议简单地从方法返回false,如果你想要的WebView做负载:
What few developers realize is that the callback is also called for subframes with non-https schemes. If you'll encounter something like <iframe src='tel:1234'>
, you will end up executing view.loadUrl('tel:1234')
and your app will show an error page, since WebView doesn't know how to load a tel:
URL.It is recommended to simply return false from the method, if you want WebView to do the loading:
public class MyWebViewClient extends WebViewClient {
...
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Returning 'false' unconditionally is fine.
return false;
}
...
}
这并不意味着在所有你不应该从 shouldOverrideUrlLoading
致电 WebView.loadUrl
。具体的模式,以避免被这样无条件地对所有的URL。
This doesn’t mean you should not call WebView.loadUrl
from shouldOverrideUrlLoading
at all. The specific pattern to avoid is doing so unconditionally for all URLs.
这篇关于Android的:如何使用时,检查URL的成功加载webview.loadUrl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!