Android webview处理404、500、断网、timeout页面的问题

2019-01-26 22:58:18

现在混合开发的app越来越多,Google对webview也越来越重视,在早期的版本webview有很多的bug,例如404、500等请求错误码我们无发直接获取(这个bug早在2008年就有人提交过issue给Google),好在Google在Android6.0修复了这个问题。根据google官网提供的最新的官网文档,我们可以重写onReceivedHttpError()方法可以捕获http Error。


    @TargetApi(android.os.Build.VERSION_CODES.M)

    @Override

    public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {

        super.onReceivedHttpError(view, request, errorResponse);

        // 这个方法在6.0才出现

        int statusCode = errorResponse.getStatusCode();

    }


这是google关于这个issue的: 

https://code.google.com/p/android/issues/detail?id=968


下面是google开发人员回复:




Google虽然在Android6.0解决了这个问题,但是Android6.0以下的手机市场占有率还是很多的,所以我们就要自己手动去解决这个问题。


处理404、500

在Android6.0以下我们是不能直接获取到404或者500的,Android6.0谷歌解决了这个问题。那么在Android6.0以下的系统我们如何处理404这样的问题呢?


两种解决方案:

方案一(推荐)通过获取网页的title,判断是否为系统默认的404页面

我们可以在WebChromeClient()子类中可以重写他的onReceivedTitle()方法


Android6.0以下判断404或者500:


    @Override

    public void onReceivedTitle(WebView view, String title) {

        super.onReceivedTitle(view, title);

        // android 6.0 以下通过title获取

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {

            if (title.contains("404") || title.contains("500") || title.contains("Error")) {

                view.loadUrl("about:blank");// 避免出现默认的错误界面

                view.loadUrl(mErrorUrl);

            }

        }

    }

1

2

3

4

5

6

7

8

9

10

11

Android6.0以上判断404或者500:


    @TargetApi(android.os.Build.VERSION_CODES.M)

    @Override

    public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {

        super.onReceivedHttpError(view, request, errorResponse);

        // 这个方法在6.0才出现

        int statusCode = errorResponse.getStatusCode();

        System.out.println("onReceivedHttpError code = " + statusCode);

        if (404 == statusCode || 500 == statusCode) {

            view.loadUrl("about:blank");// 避免出现默认的错误界面

            view.loadUrl(mErrorUrl);

        }

    }




判断断网和链接超时


    @Override

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

        super.onReceivedError(view, errorCode, description, failingUrl);

        // 断网或者网络连接超时

        if (errorCode == ERROR_HOST_LOOKUP || errorCode == ERROR_CONNECT || errorCode == ERROR_TIMEOUT) {

            view.loadUrl("about:blank"); // 避免出现默认的错误界面

            view.loadUrl(mErrorUrl);

        }

    }

1

2

3

4


方案二(不推荐) 通过HttpURLConnection请求url获取状态码

在webview加载url前先通过HttpURLConnection获取请求码,这种方案有时候不会触发shouldOverrideUrlLoading()方法,但是网上还有很多的呼声。


    private Handler mHandler = new Handler() {

        @Override

        public void handleMessage(Message msg) {

            int code = msg.what;

            if (code == 404 || code == 500) {

                System.out.println("handler = " + code);

                mWebView.loadUrl(mErrorUrl);

            }

        }

    };


    new Thread(new Runnable() {

        @Override

        public void run() {

            int responseCode = getResponseCode(mUrl);

            if (responseCode == 404 || responseCode == 500) {

                Message message = mHandler.obtainMessage();

                message.what = responseCode;

                mHandler.sendMessage(message);

            }

        }

    }).start();


    mWebView.loadUrl(mUrl);


    mWebView.setWebViewClient(new WebViewClient() {

        @Override

        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            new Thread(new Runnable() {

                @Override

                public void run() {

                    int responseCode = getResponseCode(mUrl);

                    if (responseCode == 404 || responseCode == 500) {

                        Message message = mHandler.obtainMessage();

                        message.what = responseCode;

                        mHandler.sendMessage(message);

                    }

                }

            }).start();

            view.loadUrl(url);

            return true;

        }

    }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

HttpURLConnection获取请求状态码


    /**

     * 获取请求状态码

     *

     * @param url

     * @return 请求状态码

     */

    private int getResponseCode(String url) {

        try {

            URL getUrl = new URL(url);

            HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();

            connection.setRequestMethod("GET");

            connection.setReadTimeout(5000);

            connection.setConnectTimeout(5000);

            return connection.getResponseCode();

        } catch (IOException e) {

            e.printStackTrace();

        }

        return -1;

    }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

方案三(推荐) 服务器后台是能获取404、500的,最好的办法是让后台捕获

这里就不说了


测试用的500的test.jsp页面


  • 2019-11-07 08:47:00

    详解vue2.6插槽更新v-slot用法总结

    在 2.6.0 中,我们为具名插槽和作用域插槽引入了一个新的统一的语法 (即 v-slot 指令)。这篇文章主要介绍了详解vue2.6插槽更新v-slot用法总结,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  • 2019-11-08 09:34:46

    CSS3 Transition详解和使用

    Transition 可以设置 CSS 属性的过渡效果,它有以下几个属性。 transition-property 用于指定应用过渡属性的名称 transition-duration 用于指定这个过渡的持续时间 transition-delay 用于指定延迟过渡的时间 transition-timing-function 用于指定过渡的类型 transition-property transition-property 用于指定应用过渡的属性名称,可以指定多个属性名称,多个属性名称之间用, 分隔。 默认值为 all 也就是所有的元素都应用过渡效果。 例如,想让容器的宽高有一个过渡的效果,就可以这样写:

  • 2019-11-09 19:16:35

    java标记过期方法

    java注解:@Deprecated(不建议使用的,废弃的);@SuppressWarnings(忽略警告,达到抑制编译器产生警告的目的)

  • 2019-11-12 02:56:39

    使用.htaccess重定向后无法显示图片,CSS失效,该如何处理

    现在我需要把这个域名泛解析到blog目录(*.mydomain.org),同时保持另外两个目录的解析不变。尝试对最后一段作以下修改后(前面的内容不变),出现问题:另两个目录中的网站内的图片无法显示,CSS全部失效。

  • 2019-11-14 11:21:34

    vue中的this指向问题

    ※ 对于普通函数(包括匿名函数),this指的是直接的调用者,在非严格模式下,如果没有直接调用者,this指的是window。showMessage1()里setTimeout使用了匿名函数,this指向 window。 ※ 箭头函数是没有自己的this,在它内部使用的this是由它定义的宿主对象决定。showMessage2()里定义的箭头函数宿主对象为vue实例,所以它里面使用的this指向vue实例。