Android webview onReceivedError()不起作用

时间:2012-10-29 11:38:22

标签: android android-webview

我正在尝试在我的网页视图上显示一个警告框而不是“网页不可用”错误页面,但即使按照文档添加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;  
        } 
  });
 }

提前致谢...

2 个答案:

答案 0 :(得分:3)

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

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

很快就会更新细节。 祝你好运!

答案 1 :(得分:1)

我最终找到了这样做的方法,但它并不漂亮。您可以通过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);
    }
}