【Android】WebView:onReceiveError的应用与变迁

onReceiveError是WebViewClient提供的方法,用于网页产生错误时进行回调处理。

1. 旧版的onReceiveError

在API23之前,该方法的签名是:

public void onReceivedError(WebView view, int errorCode,String description, String failingUrl);

文档是:

Report an error to the host application. These errors are unrecoverable (i.e. the main resource is unavailable). The errorCode parameter corresponds to one of the ERROR_* constants.

简单来说,onReceivedError只有在遇到不可用的(unrecoverable)错误时,才会被调用)。
比如,当WebView加载链接www.barryzhang.com时,"不可用"的情况有可以包括有:

而下面的情况则不会被报告:

  • 网页内引用其他资源加载错误,比如图片、css不可用
  • js执行错误

2. 应用:显示个自定义ERROR界面

基于以上特性,所以它可以用来处理网页加载不出来的情况,比如显示一段友好的提示语、一个重试按钮等。
比如像这样:

@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
    super.onReceivedError(view, errorCode, description, failingUrl);
    layoutError.setVisibility(View.VISIBLE); 
    textViewErrorMessage.setText("(错误码:" + errorCode + "  " + description + ")"  );
}

——这么做的还有一个原因是,虽然默认的网页错误样式每个ROM都可能不一样,但是却是一样的丑……,来个对比图感受一下,从左到右依次是:MIUI(Android5.0.2)、Nexus5X(Android7)、以及自定义之后的效果:

对比图

3. 新版的onReceiveError

So far so good, but~
API23(Android6),Google对onReceiveError进行了一次改版重载,并且把老版本的废弃了,ㄒoㄒ~~
签名改成了这样:

public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error);

文档改成了:

Report web resource loading error to the host application. These errors usually indicate inability to connect to the server. Note that unlike the deprecated version of the callback,the new version will be called for any resource (iframe, image, etc), not just for the main page. Thus, it is recommended to perform minimum required work in this callback.

新版的onReceiveError能接收到的错误更多,不再局限于之前的"不可用"的情况——好像是比之前更强大了。
但是,这时候如果我们依然用使用旧版本的方式来使用新版,像这样:

@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
    // !!在新版的onReceivedError中,沿用之前的处理逻辑(这是错误的示例!!)
    super.onReceivedError(view, errorCode, description, failingUrl);
    layoutError.setVisibility(View.VISIBLE); 
    textViewErrorMessage.setText("(错误码:" + error.getErrorCode() + "  " + error.getDescription().toString() + ")"  );
}

这会导致的问题是:在Android6以上的机器上,网页中的任意一个资源获取不到(比如字体),网页就很可能显示自定义的错误界面。尤其是如果Html用了本地化技术,'ERR_FILE_NOT_FOUND'开始变得特别常见。

4. 如何像在老版本一样工作?

4.1 继续用老版本呢?

Bingo!可以,起码从目前来看,测试结果表明至少在Andoid6以及Android7上是可以工作的。
然而,终究,使用已废弃的API终究是不太优雅——说不定哪个版本就突然不能用了,仿佛像个定时炸弹一样。

4.2 isForMainFrame

我们注意到新版的onReceivedError跟老版相比,多了一个WebResourceRequest参数,而WebResourceRequest有一个方法叫做isForMainFrame,描述为:

Gets whether the request was made for the main frame
获取当前的网络请求是否是为main frame创建的.

加上这个条件判断是来试试?

    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        super.onReceivedError(view, errorCode, description, failingUrl);
        if(request.isForMainFrame()){// 在这里加上个判断
            // 显示错误界面
        }
    }

实验证明这个方法是有效的。

4.3 当然,也还有其他方法

可以这样,直接上代码:

@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
    super.onReceivedError(view, errorCode, description, failingUrl);
     if (request.getUrl().toString() .equals(getUrl())) {// 在这里加上个判断
        // 显示错误界面
    }
}

原理是:用请求的url来判断,如果出错的url跟webView当前加载的url一致,就显示错误页面。
↑↑经测试,也能通过~

总结

总而言之,最终的代码这样写,可以同时兼容新旧版本:

    // 旧版本,会在新版本中也可能被调用,所以加上一个判断,防止重复显示
    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        super.onReceivedError(view, errorCode, description, failingUrl);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            return;
        }   
        // 在这里显示自定义错误页
    }

    // 新版本,只会在Android6及以上调用
    @TargetApi(Build.VERSION_CODES.M)
    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { 
        super.onReceivedError(view, request, error);
        if (request.isForMainFrame()){ // 或者: if(request.getUrl().toString() .equals(getUrl()))
            // 在这里显示自定义错误页
        }
    }

Also in:
http://www.barryzhang.com/
https://barryhappy.github.io/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,837评论 18 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,860评论 25 708
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,925评论 6 342
  • 作一首诗,难以提字。 说为情意,难以开口。 七情六欲,都是浮云。(试练) 至古到今,都是炼狱。(他笑)
    小叶童阅读 284评论 0 0
  • 时隔14年,王菲和谢霆锋的锋菲恋终于画上了圆满的句号。很多网友都在感慨:虽然几度沉浮,虽然年过40,但王菲风采依旧...
    戒咖啡cxj阅读 397评论 0 0