2012-10-29 134 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); 
    } 
} 
相關問題