简体   繁体   English

onReceivedError 在 android Marshmallow 及以下版本中不起作用

[英]onReceivedError not working in android Marshmallow and below

I am trying to display a custom error page using onReceivedError but it is not showing error.我正在尝试使用onReceivedError显示自定义错误页面,但它没有显示错误。 HTML it shows on default webpage not available error only HTML 它仅显示在默认网页上不可用错误

    webView.setWebViewClient(new WebViewClient(){

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error){
                webView.loadUrl("file:///android_asset/error.html");
                super.onReceivedError(view, request, error);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon){
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url){
                swipeRefreshLayout.setRefreshing(false);
                super.onPageFinished(view,url);
                progressBar.setVisibility(View.GONE);
            }
        });

error.html file is error.html 文件是

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body style="text-align:center;font-size:16px;">
<img src="05.png" width="100%">
<div>
    <p>Please Check your internet Connection</p>
    <p>Please Check Wifi</p>
    <p>There is no response from server</p>
</div>
<button style="background-color:#3700B3;color:#fff;border:0;border-radius:5px;padding:7px 15px;" onclick="Reload();">Try Again</button>
<script>
    function Reload(){
     location.reload();
    }
</script>
</body>
</html>

How can I solve this, please guide me.我该如何解决这个问题,请指导我。 Thanks.谢谢。

This question has been around for quite a long time, and probably you already got the answer.这个问题已经存在了很长时间,可能您已经得到了答案。 But as a newbie enthusiast, I will try to answer your question to help other newbie who might face same problem as yours.但是作为新手爱好者,我会尝试回答您的问题,以帮助其他可能面临与您相同问题的新手。

Your code actually is fine already.你的代码实际上已经很好了。 There is only one tiny problem.只有一个小问题。 Always call onReceivedError only after onPageFinished.始终仅在 onPageFinished 之后调用 onReceivedError。 Don't ever call onReceivedError before onPageFinished.永远不要在 onPageFinished 之前调用 onReceivedError。

You might also want to call onReceivedHttpError to make sure all errors are handled by your html asset.您可能还想调用 onReceivedHttpError 以确保所有错误都由您的 html 资产处理。

So your code should be as below.所以你的代码应该如下。

webView.setWebViewClient(new WebViewClient(){

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon){
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url){
                swipeRefreshLayout.setRefreshing(false);
                super.onPageFinished(view,url);
                progressBar.setVisibility(View.GONE);
            }
            
            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, 
                                        WebResourceError error){
                webView.loadUrl("file:///android_asset/error.html");
                super.onReceivedError(view, request, error);
            }

            @Override
            public void onReceivedHttpError(WebView view, WebResourceRequest request,
                                            WebResourceResponse errorResponse) {
                webView.loadUrl("file:///android_asset/error.html");
                super.onReceivedHttpError(view, request, errorResponse);
            }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM