Khawar Raza
Khawar Raza

Reputation: 16120

Android HttpPost Request Timeout

In my application, I have to get data from 4 different urls and then after the completion of fetching data, I have to show items in a particular sequence. I am using HttoPost to send post request. I am sending 4 requests each in a different thread. When a thread get data, it increment a count. When count reaches to 4, it means that all the 4 threads have fetched data. The problem is that sometimes one of the four threads don't respond means the defaultHttpClient.execute(post) don't return. Due to this, my count don't reaches 4 even it don't throws exception and only wait dialog keeps showing. I want that after a fixed time, it must return regardless of whether it get response from server or not. Any idea?

Upvotes: 1

Views: 12294

Answers (1)

Khawar Raza
Khawar Raza

Reputation: 16120

Its not working. I am using the following class:

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

Upvotes: 4

Related Questions