Bhabani Shankar
Bhabani Shankar

Reputation: 1285

Timeout in DefaultHttpClient Class Android

I have created an Android application where I connect to a remote server php file to retrieve some information. Below is the code for that.

Here I want to add timeout with the connection such as the connection will timeout in 5 seconds.

Any idea how to do this.

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name","test"));

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://mysite.com/test.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost); 

Regards,

Shankar

Upvotes: 11

Views: 14085

Answers (1)

Shlublu
Shlublu

Reputation: 11027

Use the HttpConnectionParams of your DefaultHttpClient::

final HttpParams httpParameters = yourHttpClient.getParams();

HttpConnectionParams.setConnectionTimeout(httpParameters, connectionTimeOutSec * 1000);
HttpConnectionParams.setSoTimeout        (httpParameters, socketTimeoutSec * 1000);

Upvotes: 33

Related Questions