【问题标题】:Android HttpPost Request TimeoutAndroid HttpPost 请求超时
【发布时间】:2012-01-30 12:43:50
【问题描述】:

在我的应用程序中,我必须从 4 个不同的 url 获取数据,然后在完成获取数据后,我必须以特定顺序显示项目。我正在使用 HttoPost 发送发布请求。我在不同的线程中分别发送 4 个请求。当一个线程获取数据时,它会增加一个计数。当 count 达到 4 时,表示 4 个线程都取到了数据。问题是有时四个线程之一不响应意味着 defaultHttpClient.execute(post) 不返回。因此,我的计数没有达到 4,即使它没有抛出异常并且只有等待对话框继续显示。我希望在固定时间后,无论是否从服务器获得响应,它都必须返回。有什么想法吗?

【问题讨论】:

标签: android timeout http-post


【解决方案1】:

它不工作。我正在使用以下课程:

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

【讨论】:

    【解决方案2】:

    使用

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-23
      • 1970-01-01
      • 2018-01-05
      • 2011-02-26
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      相关资源
      最近更新 更多