Android:如何在使用 webview.loadUrl 时检查 url 是否成功加载 [英] Android: How to check for successful load of url when using webview.loadUrl

查看:158
本文介绍了Android:如何在使用 webview.loadUrl 时检查 url 是否成功加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 webview android 中,我试图加载一个 url,为了检查这个 url 的加载是否成功完成(互联网连接可用,服务器已启动等)我的印象是 webview.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.

首先,为了检测任何阻止 WebView 连接到服务器以加载您的主页的问题(例如错误的域名、I/O 错误等),您应该使用 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 中获得的内容将是服务器错误页面.人们建议如何处理它的不同方法,请参阅此处的答案:​​如何从 Android WebView 检查页面是否为404 页面未找到"? 基本上,这实际上取决于您的期望成为好"页面和错误"页面.遗憾的是,该应用目前无法从 WebView 获取 HTTP 响应代码.

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.onPageStartedWebViewClient.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='tel:1234'> 的内容,您将最终执行 view.loadUrl('tel:1234') 和您的应用程序将显示错误页面,因为 WebView 不知道如何加载 tel: URL.如果您希望 WebView 进行加载,建议简单地从方法中返回 false:

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:如何在使用 webview.loadUrl 时检查 url 是否成功加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆