There are two kinds of timeout:
- Connection Timeout
that is the time until a connection is established
- Socket Timeout
that is the timeout for waiting for data to be received, setting any of them to 0 means infinite timeout, and it's the default value of both, setting one of them doesn't affect the other.
try{
BasicHttpParams httpParams = new BasicHttpParams();
//this will set socket timeout
HttpConnectionParams.setSoTimeout(httpParams, /*say*/ 3000);
//this will set connection timeout
HttpConnectionParams.setConnectionTimeout(httpParams, 3000);
client = new DefaultHttpClient(httpParams);
String url = "some-url";
HttpGet httpGet = new HttpGet(url);
response = httpClient.execute(httpGet);
//here use the received response
}
catch(ConnectTimeoutException ex) {
//handle connection timeout here
}
catch(SocketTimeoutException ex) {
//handle socket timeout here
}