在我的應用程序中,我必須從4個不同的URL獲取數據,然後在獲取數據完成後,我必須以特定順序顯示項目。我正在使用HttoPost發送發佈請求。我使用不同的線程發送4個請求。當一個線程獲取數據時,它會增加一個計數。當計數達到4時,意味着所有4個線程都獲取了數據。問題是,有時四個線程中的一個不響應意味着defaultHttpClient.execute(post)不返回。由於這個原因,即使不拋出異常,我的計數也不會達到4,只有等待對話框一直顯示。我希望在固定的時間之後,無論它是否從服務器獲得響應,它都必須返回。任何想法?Android HttpPost請求超時
1
A
回答
4
它不工作。我使用下面的類:
public class ConnectionManager {
private ArrayList <NameValuePair> params;
private ArrayList <NameValuePair> headers;
private String url;
public ConnectionManager(String url) {
this.url = url;
params = new ArrayList<NameValuePair>();
headers = new ArrayList<NameValuePair>();
}
public void addParam(String name, String value)
{
params.add(new BasicNameValuePair(name, value));
}
public void addHeader(String name, String value)
{
headers.add(new BasicNameValuePair(name, value));
}
public String sendRequest() throws Exception {
String serverResponse = "";
HttpPost httpPostRequest = new HttpPost(url);
httpPostRequest.getParams().setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.FALSE);
//add headers
for(int i = 0; i < headers.size(); i++) {
StringEntity entity = new StringEntity(headers.get(i).getValue(),"UTF-8");
httpPostRequest.setEntity(entity);
}
if(!params.isEmpty()){
HttpEntity httpEntity = new UrlEncodedFormEntity(params,HTTP.UTF_8);
httpPostRequest.setEntity(httpEntity);
}
serverResponse = executeRequest(httpPostRequest);
return serverResponse;
}
private String executeRequest(HttpUriRequest request) throws Exception {
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 3000);
HttpConnectionParams.setSoTimeout(params, 10000);
DefaultHttpClient client = new DefaultHttpClient(params);
HttpResponse httpResponse;
String serverResponse = "";
httpResponse = client.execute(request);
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
serverResponse = convertStreamToString(instream);
instream.close();
}
Log.d("server response", serverResponse);
return serverResponse;
}
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
1
使用
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 3000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
相關問題
- 1. Android上的Slow HttpPost請求
- 2. 關於Android HttpPost請求
- 3. Loop HttpPost請求
- 4. 超時http請求? Android
- 5. 請求超時 - 請求超時
- 6. HttpPost請求響應
- 7. NSURL請求超時請求
- 8. 發送HttpPost請求時的HttpEntity概念
- 9. httppost超時錯誤
- 10. 請求超時
- 11. javax.net.ssl.SSLException在Android中使用HttpPost請求
- 12. 使用JSON Body進行HTTPPost請求,Android
- 13. Android HttpPost請求到服務器
- 14. Android HttpPost請求無法發佈
- 15. 如何使httppost請求
- 16. HTTPClient不發送HttpPost請求
- 17. 發送HttpPost請求時發生SocketTimeOutException
- 18. Corba請求超時
- 19. KSoap請求超時?
- 20. HTTP請求超時
- 21. NSURLSession - 請求超時
- 22. Ajax請求超時
- 23. Web請求超時
- 24. IIS請求超時
- 25. SharePoint - 「請求超時」
- 26. Tomcat請求超時
- 27. 超時ASIHTTP請求
- 28. NSXMLParser - 請求超時
- 29. 請求超時:檢索文件時GET請求超時
- 30. Android的HttpPost超時異常不工作
設置連接超時喜歡這裏:http://stackoverflow.com/questions/693997/how-to-set-httpresponse-timeout-for-android-in-java – edovino 2012-01-30 12:53:31
和使用CountDownLatch作爲初始值爲4的計數器。 – Jens 2012-01-30 13:09:39
對不起。 CountDownLatch是什麼意思? – 2012-01-30 13:20:00