简体   繁体   English

Android webview onReceivedError()不起作用

[英]Android webview onReceivedError() not working

I'm trying to show an alert box instead of 'Web page not available' error page on my web view but not working even after adding onReceivedError() method as per documentation, please suggest me if I'm missing something, Here is my code... 我正在尝试在我的网页视图上显示一个警告框而不是“网页不可用”错误页面,但即使按照文档添加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;  
        } 
  });
 }

Thanks in Advance... 提前致谢...

I am not sure of what your error is but the documentation of onReceivedError is somewhat missleading. 我不确定你的错误是什么,但onReceivedError的文档有点误导。 At this point you CANNOT intercept HTTP error responses using this method. 此时,您无法使用此方法拦截HTTP错误响应。 check these posts . 查看这些帖子

I am developing a similar workaround using the method described in this other post . 我正在使用此其他帖子中描述的方法开发类似的解决方法。

Will update with the details soon. 将很快更新详细信息。 Good luck! 祝好运!

I eventually found a way of doing this, but it isn't pretty. 我最终找到了一种方法,但它并不漂亮。 You can load the page in javascript via an XMLHttpRequest, which lets you access the status code. 您可以通过XMLHttpRequest在javascript中加载页面,这样您就可以访问状态代码。 This probably messes up the progress notifications and may have other undesirable behaviour too. 这可能会混淆进度通知,也可能有其他不良行为。 But it was acceptable in my app, so it may be of some help to others. 但它在我的应用程序中是可以接受的,因此它可能对其他人有所帮助。

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);
    }
}

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

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