2011-04-15 66 views
1

我試圖訪問的網頁發回403錯誤。我期待WebViewClient收到一個錯誤,但它沒有。它不應該得到錯誤?即使服務器發回錯誤,WebView也不會收到錯誤

engine.setWebViewClient(new WebViewClient() { 
     boolean error = false; 
     public void onReceivedError(WebView view, int errorCode, 
       String description, String failingUrl) { 

      //IT DOESN'T COME HERE even IF SERVER SENDS BACK 403 ERROR 
     } 
    }); 

它接縫像,我將不得不使用的HttpRequest讀取響應和解析響應檢查這是怎麼回事。有沒有其他方法可以做到這一點?我可以從WebView中檢查標題嗎?

回答

2

我不得不使用兩個請求來實現這個需求。首先請求檢查證書是否正常,並且沒有錯誤(使用HttpURLConnection)。只有在響應正常的情況下,我才能加載web視圖。

protected Void doInBackground(final String... url) { 
     boolean failed = false; 
     int error = 0; 

     try { 
      URL urlToLoad = new URL(url[0]); 
      HttpURLConnection conn = (HttpURLConnection) urlToLoad 
        .openConnection(); 
      int response = conn.getResponseCode(); 
      if (response != HttpURLConnection.HTTP_OK) { 
       error = response; 
       failed = true; 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     if (!failed) { 
      webView.loadUrl(url[0]); 
     } else { 
      handler.post(hideProgressRunnable); 
      connecting = false; 
      showError(ErrorMap.getErrorForHttpCode(error)); 
     } 
     return null; 
    } 
+0

傷心但真實。請考慮針對此問題主播Android問題:http://code.google.com/p/android/issues/detail?id=32755&q=webview%20error&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars – larham1 2012-08-27 02:43:57

相關問題