2012-10-29 101 views
3

我试图显示一个警告框,而不是'Web页面不可用'错误页面在我的web视图上,但不工作,即使按照文档添加onReceivedError()方法后,请建议我如果我失去了一些东西,这是我的代码...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; 
     } 
    }); 
} 

在此先感谢...

+0

你的实际错误是什么? –

回答

3

我不知道一个什么样的错误,但onReceivedError的文档有些missleading。 此时您不能使用此方法拦截HTTP错误响应。 检查these posts

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

将尽快更新细节。 祝你好运!

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); 
    } 
} 
相关问题