0

I am trying to put an entry in database. This is the code I am using

String url = String.format("http://xxxxxxx/xxx/index.php?home=yes&pid=%s", pid);

        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);

    try {
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String response = httpclient.execute(httppost, responseHandler);

        Log.e("abc", response);
        return response;
        } catch (ClientProtocolException es) 
        {   
            Log.e("x" , es.getMessage());
            return "";
        } catch (IOException e) 
        {
            Log.e("aasx" , e.getMessage());
            return "";
        }

This code works fine. Now when there is an internet connection problem, the code stuck at httpclient.execute(httppost, responseHandler); for more than 2 minutes.

Is there any way in which it check for only few seconds and if no resposne then catch the exceptions?

Muhammad Umar
  • 11,391
  • 21
  • 91
  • 193

2 Answers2

1

Your question is similar to the one here.

You just need to set a timeout for the HttpClient as shown in the answers there. This can be done by passing in an HttpParams object to the HttpClient constructor.

Community
  • 1
  • 1
onit
  • 6,306
  • 3
  • 24
  • 31
0

I'm not too familiar with Java, but in doing something similar in Actionscript 3, there is a timeout number you can usually put on an HTTP request to stop trying after a certain amount of time. I usually set this to 15-30 seconds for requests that should return quickly.

Seth
  • 1,353
  • 1
  • 8
  • 17