【问题标题】:How can I show a custom "webpage not available" page in Android WebView?如何在 Android WebView 中显示自定义“网页不可用”页面?
【发布时间】:2012-12-26 11:53:52
【问题描述】:

WebView 中没有可用的连接页面/警报时,我要做一些事情(例如,加载本地 html 页面或警报)。我必须和Prevent WebView from displaying "web page not available" 一起玩,但没有任何成功。任何建议将不胜感激。

【问题讨论】:

    标签: android webview


    【解决方案1】:

    这一切都归结为简单地显示来自 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
        }
    });
    

    【讨论】:

      【解决方案2】:

      上面的代码给了我两个弃用警告,所以我建议修改如下。这在包含 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);
              }
          });
      

      【讨论】:

        【解决方案3】:
            @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");
            } 
        

        【讨论】:

        • 嘿伙计,我看到你做了一些编辑,但你只编辑了一些小问题,比如将标题中的第一个单词大写,你还应该解决问题的其他问题。我在您对this question 所做的一次编辑中详细说明了这一点,在这个问题中,您还可以将 I 和 Tessaract 大写并从代码中删除空行并删除不必要的 ...。这只是一个友好的提醒,但这样的修改迟早会被拒绝。
        【解决方案4】:

        您可以从资产加载 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);
                }
        

        如果出现问题,请原谅我。我不熟练。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多