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?
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.