5

I am using the following code to get data from the server with http request.

HttpClient client = new DefaultHttpClient();
    String URL = urlGenerator();

    StringBuilder url = new StringBuilder(URL); 
    HttpGet get = new HttpGet(url.toString());

    HttpResponse response = client.execute(get);
    int status = response.getStatusLine().getStatusCode();

    if(status == 200){
            ...
            }

Its working fine.But in case if the phone is connected to wifi or gprs 3g but internet is not working or internet connection is not there, i want to use the timeout feature in the above code.

say after 3 sec i want to show timeout please try again.. how do i do that. in case of time out i want to show a text in the textviw connection timeout .. how do i do that please help

Saurabh Sinha
  • 1,772
  • 4
  • 27
  • 54

1 Answers1

3

use this code to accomplish your task

HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 30000);
HttpConnectionParams.setSoTimeout(httpParameters, 30000);
Prakhar
  • 2,270
  • 19
  • 26
  • how do i know that time out happened .. i mean to say .. in case of time out i want to print a message "check your connection" how do i do that – Saurabh Sinha Aug 13 '13 at 11:25