简体   繁体   English

如何在 Android WebView 中显示自定义的“网页不可用”页面?

[英]How can I show a custom "webpage not available" page in Android WebView?

I'm out to do something when no connection is available page/alert in WebView (eg load a local html page or alert).WebView没有可用的页面/警报(例如加载本地 html 页面或警报)时,我要做一些事情。 I've to play with Prevent WebView from displaying "web page not available" but without any success.我必须玩防止 WebView 显示“网页不可用”但没有任何成功。 Any suggestions would be appreciated.任何建议,将不胜感激。

It all came down to simply showing an AlertDialog from onReceivedError: 这一切都归结为只是从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);
                }

If you're new to WebView, you'll be looking to implement onReceivedError like this: 如果您是WebView的新手,那么您将希望实现onReceivedError,如下所示:

mWebView.setWebViewClient(new WebViewClient() {
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        //Code here
    }
});

The above code gives me two deprecation warnings, so I would suggest modifying it as follows. 上面的代码给了我两个弃用警告,所以我建议修改如下。 This goes in the activity that contains the WebView component: 这包含在包含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);
        }
    });
    @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");
    } 

You can load html file from assets.您可以从资产加载 html 文件。 Put your html like below.把你的html像下面一样。 assets/html/no_connection.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);
        }

The following one is deprecated in api 23以下一个在 api 23 中已弃用

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

Forgive me if something went wrong.如果出了什么问题,请原谅我。 I am not proficient.我不熟练。

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

相关问题 如何将网页不可用页面替换为自定义页面? (网页浏览) - How to Replace Webpage Not Available Page To Custom Page? (WEBVIEW) (android studio) 无法更改 webview 中的“网页不可用”页面 - (android studio) can't change the "Webpage not available" page in webview WebView组件不显示网页:网页不可用 - WebView component not show web page: Webpage not available 网页不可用,WebView Android - Webpage not available, WebView Android Android Webview不显示网页,但我仍然可以与之交互 - Android Webview doesn't show webpage but I can still interact with it 如何使用 ReactNative WebView 组件在我的 Android 应用程序中显示网页的特定部分? - how can I use ReactNative WebView component to show a specific part of a webpage in my Android app? WebView打开网页,单击链接显示“网页不可用” - WebView open webpage, links when clicked show “Web page not available” 如何在android的webview中显示网页的特定元素? - How to show specific elements of webpage in webview in android? 如何在HTC的android中停止缩放Webview的网页 - How can I stop zoom in webpage of webview in android for HTC 如何在Android中使用Google chrome而非WebView加载网页? - How can I load a webpage with Google chrome instead of WebView in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM