Timeout in DefaultHttpClient Class Android [duplicate]
I have created an Android application where I connect to a remote server php file to retrieve some information. Below is the code for that.
Here I want to add timeout with the connection such as the 开发者_如何学Goconnection will timeout in 5 seconds.
Any idea how to do this.
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name","test"));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://mysite.com/test.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
Regards,
Shankar
Use the HttpConnectionParams
of your DefaultHttpClient
::
final HttpParams httpParameters = yourHttpClient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, connectionTimeOutSec * 1000);
HttpConnectionParams.setSoTimeout (httpParameters, socketTimeoutSec * 1000);
精彩评论