2010-01-31 137 views
6

我希望有人能夠幫助我斷斷續續的連接我 獲取使用HttpsURLConnection代碼。我正在使用的代碼是 如下:HttpsURLConnection和間歇性連接

HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); 
      conn.setReadTimeout(10 * 1000); 
if conn.getResponseCode() != 200) { 
      Log.v(TAG, "error code:" + conn.getResponseCode()); 
} 

的連接工作在第一時間每次,當我用它來拉 JSON文件。但是,當我再次使用連接發送命令時,第一次總是失敗。如果我快速發送 命令(在5秒鐘內),但通常會工作,但如果我等一會兒就會失敗。 我不認爲它的SSL問題,因爲它正確地連接了第一次 ,但我可能在這裏是錯的。我也嘗試過許多不同 變化,如添加:

conn.setUseCaches(false); 
conn.setRequestProperty("Connection","Keep-Alive"); 
conn.getHostnameVerifier(); 
conn.getSSLSocketFactory(); 
conn.setDoOutput(true); 
conn.setDoInput(true); 
conn.setRequestMethod("POST"); 
conn.wait(100); 

但是,我沒有運氣。任何幫助將不勝感激。

+0

如果我可以建議使用'LogCat'並在你的代碼塊中放置調試語句,以便它可以向你報告究竟是什麼失敗, e – 2010-01-31 18:31:41

+0

我從conn.getResponseCode()獲得的Logcat響應是「-1」,我搜索到了,並且在https連接的-1響應代碼中找不到任何內容。 – OliverPank 2010-01-31 21:40:29

回答

9

嘗試System.setProperty("http.keepAlive", "false");你做

HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); 
+0

完美時將其設置一次。這工作。我不知道你可以使用「系統」。我猜測SSL連接在我通過不同的活動進行移動之前保持開放。謝謝! – OliverPank 2010-02-04 04:54:39

+0

是的,我認爲這與errortream沒有被完全讀取有關。 – m6tt 2010-02-04 12:14:10

+0

http://code.google.com/p/android/issues/detail?id=2939 應該固定爲Froyo。 – 2011-10-21 18:20:17

1

試試這個代碼 - 它非常可靠地對我說:

public static final String USER_AGENT = "Mozilla/5.0 (Linux; U; Android 1.1; en-us;dream) AppleWebKit/525.10+ (KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.2"; 
private DefaultHttpClient getThreadSafeHttpClient() { 
    final HttpParams params = new BasicHttpParams(); 
    params.setParameter("http.useragent", USER_AGENT); 
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 
    HttpProtocolParams.setContentCharset(params, "UTF-8"); 
    final SchemeRegistry registry = new SchemeRegistry(); 
    registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
    final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory(); 
    sslSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 
    registry.register(new Scheme("https", sslSocketFactory, 443)); 
    final ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(params, registry); 
    final DefaultHttpClient httpclient = new DefaultHttpClient(manager, params); 
    // how to handle retries 
    final HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() { 
     public boolean retryRequest(final IOException exception, final int executionCount, final HttpContext context) { 
      if (executionCount >= 5) { 
       // Do not retry if over max retry count 
       return false; 
      } 
      if (exception instanceof NoHttpResponseException) { 
       // Retry if the server dropped connection on us 
       return true; 
      } 
      if (exception instanceof SSLHandshakeException) { 
       // Do not retry on SSL handshake exception 
       return false; 
      } 
      final HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST); 
      final boolean idempotent = !(request instanceof HttpEntityEnclosingRequest); 
      if (idempotent) { 
       // Retry if the request is considered idempotent 
       return true; 
      } 
      return false; 
     } 

    }; 
    httpclient.setHttpRequestRetryHandler(myRetryHandler); 
    return httpclient; 
} 
+0

謝謝...這比我簡單的https url連接複雜得多。我不確定如何正確使用它。由於getThreadSafeHttpClient函數沒有任何輸入,我在哪裏輸入我的url? – OliverPank 2010-02-01 02:30:33

+0

你只是用它來獲取HttpClient,然後像往常一樣使用客戶端。類似於 'HttpClient client = getThreadSafeHttpClient();' – Bostone 2010-02-01 04:33:58

+0

HttpGet get = new HttpGet(url); HttpResponse response = this.client.execute(get); – Bostone 2010-02-01 05:17:44

0

之前只是一點點除了m6tt的回答以上:

private static void disableConnectionReuseIfNecessary() { 
    // HTTP connection reuse which was buggy pre-froyo 
    if (!Constants.SUPPORTS_FROYO) { 
     System.setProperty("http.keepAlive", "false"); 
    } 
}