0

I'm trying to load information over the network on a thread. When there is no internet it will freeze for a long time before setting off a exception or just freeze.

Is there a way to set a timeout for // FREEZES HERE or takes a long time to through exception when there is no internet? Is their a way to set a timeout for response = httpclient.execute(httppost);?

HttpClient httpclient = new DefaultHttpClient();
HttpPost  httppost = new HttpPost("http://besttechsolutions.biz/projects/bookclub/getevents.php");
// FREEZES HERE or takes a long time to through exception when there is no internet
response = httpclient.execute(httppost);
responseBody = EntityUtils.toString(response.getEntity());
Jason Aller
  • 3,391
  • 28
  • 37
  • 36
Ted pottel
  • 6,503
  • 16
  • 67
  • 124
  • 1
    this might help you http://stackoverflow.com/questions/3000214/java-http-client-request-with-defined-timeout or this http://stackoverflow.com/questions/9873810/using-apache-httpclient-how-to-set-the-timeout-on-a-request-and-response – Alexi Akl Mar 12 '13 at 12:57
  • or just check internet access http://stackoverflow.com/questions/1560788/how-to-check-internet-access-on-android-inetaddress-never-timeouts – Denis Kulygin Mar 12 '13 at 13:02

1 Answers1

0

Try the following.

HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 30000);
HttpConnectionParams.setSoTimeout(httpParameters, 30000);

if(null == httpClient) 
httpClient = new DefaultHttpClient(httpParameters);

The above code generally sets a default timeout for httpClient. To check internet access, refer the link posted by Nomand.

Renjith
  • 2,918
  • 5
  • 40
  • 60