2012-01-30 130 views
1

在我的应用程序中,我必须从4个不同的URL获取数据,然后在获取数据完成后,我必须以特定顺序显示项目。我正在使用HttoPost发送发布请求。我使用不同的线程发送4个请求。当一个线程获取数据时,它会增加一个计数。当计数达到4时,意味着所有4个线程都获取了数据。问题是,有时四个线程中的一个不响应意味着defaultHttpClient.execute(post)不返回。由于这个原因,即使不抛出异常,我的计数也不会达到4,只有等待对话框一直显示。我希望在固定的时间之后,无论它是否从服务器获得响应,它都必须返回。任何想法?Android HttpPost请求超时

+0

设置连接超时喜欢这里:http://stackoverflow.com/questions/693997/how-to-set-httpresponse-timeout-for-android-in-java – edovino 2012-01-30 12:53:31

+0

和使用CountDownLatch作为初始值为4的计数器。 – Jens 2012-01-30 13:09:39

+0

对不起。 CountDownLatch是什么意思? – 2012-01-30 13:20:00

回答

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);