5

WebView当没有可用的连接页面/警报时(例如加载本地 html 页面或警报),我会做一些事情。我必须使用防止 WebView 显示“网页不可用”但没有任何成功。任何建议,将不胜感激。

4

4 回答 4

5

这一切都归结为简单地显示来自 onReceivedError 的 AlertDialog:

 @Override
 public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
                    //Clearing the WebView
                    try {
                        webView.stopLoading();
                    } catch (Exception e) {
                    }
                    try {
                        webView.clearView();
                    } catch (Exception e) {
                    }
                    if (webView.canGoBack()) {
                        webView.goBack();
                    }
                    webView.loadUrl("about:blank");

                    //Showing and creating an alet dialog
                    AlertDialog alertDialog = new AlertDialog.Builder(youractivity.this).create();
                    alertDialog.setTitle("Error");
                    alertDialog.setMessage("No internet connection was found!");
                    alertDialog.setButton("Again", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int which) {
                           finish();
                           startActivity(getIntent());
                       }
                    });

                    alertDialog.show();

                    //Don't forget to call supper!
                    super.onReceivedError(webView, errorCode, description, failingUrl);
                }

如果您是 WebView 的新手,您将希望像这样实现 onReceivedError:

mWebView.setWebViewClient(new WebViewClient() {
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        //Code here
    }
});
于 2012-12-26T17:00:45.823 回答
4

上面的代码给了我两个弃用警告,所以我建议如下修改它。这进入包含 WebView 组件的活动中:

    myWebView.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
            try {
                webView.stopLoading();
            } catch (Exception e) {
            }

            if (webView.canGoBack()) {
                webView.goBack();
            }

            webView.loadUrl("about:blank");
            AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
            alertDialog.setTitle("Error");
            alertDialog.setMessage("Cannot connect to the R2R Server. Check your internet connection and try again.");
            alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Try Again", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                    startActivity(getIntent());
                }
            });

            alertDialog.show();
            super.onReceivedError(webView, errorCode, description, failingUrl);
        }
    });
于 2014-11-18T15:33:28.150 回答
0

您可以从资产加载 html 文件。把你的 html 放在下面。资产/html/no_connection.html

@Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error)
        {
Toast.makeText(getActivity(),String.valueOf(error.getErrorCode())+":"+ error.getDescription(),Toast.LENGTH_LONG).show();
            wv.loadUrl("file:///android_asset/html/no_connection.html");
            super.onReceivedError(view, request, error);
        }

以下一项在 api 23 中已弃用

@Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
        {
            // TODO: Implement this method
            super.onReceivedError(view, errorCode, description, failingUrl);
        }

如果出了什么问题,请原谅我。我不熟练。

于 2021-11-04T08:04:10.567 回答
0
    @Override 
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        super.onReceivedError(view, errorCode, description, failingUrl);

// declare a text view in your xml
                sample_TextView.setText(R.string.no_internet_connection);

        view.loadUrl("about:blank");
    } 
于 2017-08-23T00:16:06.117 回答