自從我在ICS上測試我的應用程序後,我面臨一個奇怪的問題。Android 4.0 org.apache.http.conn.ConnectTimeoutException:連接到...超時
使用在Android 2.X下面的代碼效果很好(有時超時發生,但很少倍):
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParameters, SOCKET_TIMEOUT);
final DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
// Create a new HttpClient and Post Header
HttpPost httpPost = new HttpPost(url);
HttpResponse response = null;
try {
if (keys != null) {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
}
// Cookies
// Create a local instance of cookie store
if (checkCookieValues()) {
BasicClientCookie cookieSession = new BasicClientCookie(mCookieName, mCookieValue);
cookieSession.setDomain(mCookieDomain);
httpClient.getCookieStore().clear();
httpClient.getCookieStore().addCookie(cookieSession);
}
// Execute HTTP Post Request
response = httpClient.execute(httpPost);
httpClient.getConnectionManager().shutdown();
} catch (UnknownHostException e) {
Log.e(TAG, "Error when calling postData", e);
} catch (SocketTimeoutException e) {
Log.e(TAG, "Error when calling postData", e);
} catch (ClientProtocolException e) {
Log.e(TAG, "Error when calling postData", e);
} catch (SocketException e) {
Log.e(TAG, "Error when calling postData", e);
} catch (IOException e) {
Log.e(TAG, "Error when calling postData", e);
}
return response;
在ICS,只要我收到一個超時例外,後面所有的調用將返回超時異常。
Timeout exception received :
02-06 19:06:05.425: E/PLTV:PlanningTVService(2980): org.apache.http.conn.ConnectTimeoutException: Connect to /78.109.91.193:80 timed out
02-06 19:06:05.425: E/PLTV:PlanningTVService(2980): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121)
02-06 19:06:05.425: E/PLTV:PlanningTVService(2980): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
02-06 19:06:05.425: E/PLTV:PlanningTVService(2980): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
02-06 19:06:05.425: E/PLTV:PlanningTVService(2980): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
02-06 19:06:05.425: E/PLTV:PlanningTVService(2980): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
02-06 19:06:05.425: E/PLTV:PlanningTVService(2980): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
02-06 19:06:05.425: E/PLTV:PlanningTVService(2980): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
02-06 19:06:05.425: E/PLTV:PlanningTVService(2980): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
02-06 19:06:05.425: E/PLTV:PlanningTVService(2980): at com.kreactive.planningtv.service.PlanningTVService.postData(PlanningTVService.java:1554)
02-06 19:06:05.425: E/PLTV:PlanningTVService(2980): at com.kreactive.planningtv.service.PlanningTVService.fbConnect(PlanningTVService.java:1897)
02-06 19:06:05.425: E/PLTV:PlanningTVService(2980): at com.kreactive.planningtv.service.PlanningTVService.onHandleIntent(PlanningTVService.java:569)
02-06 19:06:05.425: E/PLTV:PlanningTVService(2980): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
02-06 19:06:05.425: E/PLTV:PlanningTVService(2980): at android.os.Handler.dispatchMessage(Handler.java:99)
02-06 19:06:05.425: E/PLTV:PlanningTVService(2980): at android.os.Looper.loop(Looper.java:137)
02-06 19:06:05.425: E/PLTV:PlanningTVService(2980): at android.os.HandlerThread.run(HandlerThread.java:60)
有沒有人遇到過這個問題?有沒有辦法避免這個問題? 我查了一下,但沒有找到類似的問題(尤其是ICS)。
感謝您的回覆!
你能爲此獲得任何解決方案嗎 – CoronaPintu 2013-11-22 04:54:34
你是否在獨特的線程中運行這個代碼? – 2014-04-28 19:30:34
我在後臺線程上有類似的代碼,並且每當將我的目標更改爲4.X時都會注意到相同的問題。 ConnectTimeoutExceptions隨機拋出,但經常。當瞄準2.X – Chicowitz 2014-04-28 19:32:16