开发者

Timeout in DefaultHttpClient Class Android [duplicate]

This question already has answers here: How to set HttpResponse timeout for Android in Java (10 answers) Closed 3 days ago.

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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜