2013-06-28 45 views
0

如果連接或套接字超時,我使用HttpClientHttpRequestRetryHandler重試。因此,我關閉了連接並調用預計要調用HttpRequestRetryHandler的API。但它沒有被調用。甚至沒有輸入該方法。HttpRequestRetryHandler未被調用

代碼:

HttpClient client = new DefaultHttpClient(); 
client.getParams().setIntParameter(
HttpConnectionParams.CONNECTION_TIMEOUT, 
      CONNECTION_TIMEOUT * 1000); 
client.getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT, 
      SOCKET_TIMEOUT * 1000); 

// Retry handler which handles request retry 
HttpRequestRetryHandler requestRetryHandler = new HttpRequestRetryHandler() { 

    @Override 
    public boolean retryRequest(IOException exception, 
       int executionCount, HttpContext context) { 

     if ((exception instanceof ConnectTimeoutException || exception instanceof SocketTimeoutException) 
        && executionCount <= MAX_TRIES) { 

      Log.d(TAG, "Retrying connection."); 
      return true; 

     } 

     return false; 
    } 
}; 

((AbstractHttpClient) client) 
      .setHttpRequestRetryHandler(requestRetryHandler); 

但在同一時間,我試圖抓住ConnectTimeoutException如下圖所示,和執行確實進入了catch塊爲ConnectTimeoutException

try { 

    // Making API request 

} catch (ConnectTimeoutException ce) { 

    // Execution enters here 
    ce.printStackTrace(); 
} 

我的avd在android版本果凍bean上運行。

代碼有什麼問題嗎?打印ConnectTimeoutException跟蹤時

logcat的:

enter image description here

回答

2

我有同樣的問題,我想分享我的解決方案(爲我工作)。

許多通常使用的HTTP服務器被配置爲在一段時間不活動之後放棄持久連接,以便節約系統資源,而且通常不會通知客戶端。請參閱HttpClient Connection keep alive strategy

如果服務器斷開連接,HttpRequestRetryHandler將無法再次嘗試(執行此操作)。

所以我用這種方式解決:

  1. 實現了自定義連接的Keep Alive戰略

    mHttpClient.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() { 
        // Honor 'keep-alive' header 
        HeaderElementIterator it = new BasicHeaderElementIterator(
         response.headerIterator(HTTP.CONN_KEEP_ALIVE)); 
        while (it.hasNext()) { 
         HeaderElement he = it.nextElement(); 
         String param = he.getName(); 
         String value = he.getValue(); 
         if (value != null && param.equalsIgnoreCase("timeout")) { 
          try { return Long.parseLong(value) * 1000; } 
          catch(NumberFormatException ignore) { } 
         } 
        } 
    
        ...... 
    
        //.Keep Alive for 30 secs 
        return 30 * 1000; 
        } 
    }); 
    
  2. 然後我實現了自定義的重傳處理器:

    HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() { 
    
        public boolean retryRequest(IOException exception, 
               int executionCount, 
               HttpContext context) { 
         // retry a max of 5 times 
         if (executionCount >= 5) return false; 
    
         if (exception instanceof NoHttpResponseException) { 
          // Retry if the server dropped connection on us 
          return true; 
         } 
    
         //.TODO your custom logic... 
    
         Boolean b = (Boolean) 
          context.getAttribute(ExecutionContext.HTTP_REQ_SENT); 
         boolean sent = (b != null && b.booleanValue()); 
         if (!sent) { 
          // Retry if the request has not been sent fully or 
          // if it's OK to retry methods that have been sent 
          return true; 
         } 
    
         // otherwise do not retry 
         return false; 
        } 
    }; 
    
    mHttpClient.setHttpRequestRetryHandler(retryHandler); 
    
+0

我試過你的連接保持活着策略,請求重試處理程序似乎仍然沒有被調用,我還能做什麼?我怎樣才能確保它的工作原理? –

0
我曾經有過

同樣的問題,並解決了我的問題是使用requestSentRetryEnabled = true初始化我的自定義處理程序。即使用:代替

static DefaultHttpRequestRetryHandler myCustomHandler = new DefaultHttpRequestRetryHandler(2, true) { 
... 
} 

static DefaultHttpRequestRetryHandler myCustomHandler = new DefaultHttpRequestRetryHandler() { 
... 
}