3

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

提前致谢...

4

2 回答 2

3

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

我正在使用一篇文章中描述的方法开发类似的解决方法。

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

于 2012-11-02T14:35:29.927 回答
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);
    }
}
于 2013-04-29T00:07:29.050 回答