Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timeout in DefaultHttpClient Class Android

Tags:

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

like image 562
Bhabani Shankar Avatar asked Aug 29 '11 10:08

Bhabani Shankar


People also ask

How do I set timeout in HTTP client?

The default value is 100,000 milliseconds (100 seconds). To set an infinite timeout, set the property value to InfiniteTimeSpan. A Domain Name System (DNS) query may take up to 15 seconds to return or time out.

What is read timeout in HTTP client?

timeout) – the time waiting for data – after establishing the connection; maximum time of inactivity between two data packets. the Connection Manager Timeout (http. connection-manager. timeout) – the time to wait for a connection from the connection manager/pool.

What is the default timeout for HTTP request in Java?

The Request Timeout property specifies the number of seconds the server waits between accepting a connection to a client and receiving information from it. The default setting is 30 seconds.


1 Answers

Use the HttpConnectionParams of your DefaultHttpClient::

final HttpParams httpParameters = yourHttpClient.getParams();

HttpConnectionParams.setConnectionTimeout(httpParameters, connectionTimeOutSec * 1000);
HttpConnectionParams.setSoTimeout        (httpParameters, socketTimeoutSec * 1000);
like image 118
Shlublu Avatar answered Sep 21 '22 19:09

Shlublu