【问题标题】:Android webview onReceivedError() not workingAndroid webview onReceivedError()不起作用
【发布时间】:2023-03-23 16:58:02
【问题描述】:

我试图在我的网页视图上显示一个警告框而不是“网页不可用”错误页面,但即使在根据文档添加 onReceivedError() 方法后也无法正常工作,如果我遗漏了什么,请建议我,这是我的代码...

public class CloudPageHolder extends Activity {
WebView mWebView;
ProgressDialog _dialog ;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mWebView = (WebView) findViewById(R.id.webview);


    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setDomStorageEnabled(true);
    mWebView.loadUrl("file:///android_asset/www/MyPage.html");
    mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
    mWebView.getSettings().setPluginsEnabled(false);
    mWebView.getSettings().setSupportMultipleWindows(false);
    mWebView.getSettings().setSavePassword(false);
    mWebView.getSettings().getAllowFileAccess();
    mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    mWebView.getSettings().setUseWideViewPort(true);
    mWebView.getSettings().setLoadWithOverviewMode(true);

    mWebView.setWebViewClient(new WebViewClient() {  
        public void onReceivedError(WebView view, int errorCode, String description,     String failingUrl) {  
          new AlertDialog.Builder(CloudPageHolder.this)  
                .setMessage("Something went wrong!")  
                .setPositiveButton(android.R.string.ok,  
                        new AlertDialog.OnClickListener()  
                        {  
                           public void onClick(DialogInterface dialog, int which)  
                            { 

                               dialog.dismiss();
                            }  
                        })  
                .setCancelable(false)  
                .create()  
                .show();  
                }  
        });  


    mWebView.setWebChromeClient(new WebChromeClient() {  
        @Override  
        public boolean onJsAlert(WebView view, String url, final String message, final android.webkit.JsResult result)  
        {  
            new AlertDialog.Builder(CloudPageHolder.this)  
                .setMessage(message)  
                .setPositiveButton(android.R.string.ok,  
                        new AlertDialog.OnClickListener()  
                        {  
                           public void onClick(DialogInterface dialog, int which)  
                            { 
                               result.confirm();
                            }  
                        })  
                .setCancelable(false)  
                .create()  
                .show();  

            return true;  
        } 
  });
 }

在此先感谢...

【问题讨论】:

    标签: android android-webview


    【解决方案1】:

    我不确定您的错误是什么,但 onReceivedError 的文档有些误导。 此时,您不能使用此方法拦截 HTTP 错误响应。 检查these posts

    我正在使用this other post 中描述的方法开发类似的解决方法。

    将很快更新详细信息。 祝你好运!

    【讨论】:

      【解决方案2】:

      我最终找到了这样做的方法,但它并不漂亮。您可以通过 XMLHttpRequest 在 javascript 中加载页面,这使您可以访问状态代码。这可能会弄乱进度通知,并且可能还有其他不良行为。但它在我的应用中是可以接受的,所以它可能对其他人有一些帮助。

      public class StatusNotifyingWebView extends WebView {
      
          public interface OnStatusReceivedListener {
              void onStatusReceived(int statusCode);
          }
      
          private class JsInterface {
              public void notifyRequestStatus(final int statusCode) {
                  getHandler().post(new Runnable() {
                      @Override
                      public void run() {
                          mOnStatusReceivedListener.onStatusReceived(statusCode);
                      }
                  });
              }
          }
      
          private OnStatusReceivedListener mOnStatusReceivedListener;
      
          public void setOnStatusReceivedListener(final OnStatusReceivedListener listener) {
              mOnStatusReceivedListener = listener;
              addJavascriptInterface(new JsInterface(), "statusNotifyJsInterface");
          }
      
          public void loadUrlWithStatusEvent(final String url) {
              Assert.assertNotNull(mOnStatusReceivedListener);
              final String script =
                      "  var httpRequest = new XMLHttpRequest();\n"
                      + "httpRequest.open('GET', '" + url + "', false);\n"
                      + "try { httpRequest.send(null); } catch (err) { }\n"
                      + "statusNotifyJsInterface.notifyRequestStatus(httpRequest.status);\n"
                      + "document.write(httpRequest.responseText);\n"
                      + "document.close();\n";
      
              final String html = "<html><head><script>" + script + "</script></head><body/></html>";
              loadDataWithBaseURL(url, html, "text/html", "UTF-8", null);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-29
        • 2012-12-29
        • 2011-06-27
        • 2018-11-09
        • 2018-11-14
        • 2015-03-22
        • 1970-01-01
        • 2015-08-16
        相关资源
        最近更新 更多