超时DefaultHttpClient级的Andr​​oidDefaultHttpClient、Andr、oid

2023-09-13 00:25:30 作者:失恋王

我已经创建了一个Android的应用程序,我连接到​​远程服务器的PHP文件来获取一些信息。下面是code为。

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.

在这里我想补充暂停时以作为连接将在5秒超时的连接等。

Here I want to add timeout with the connection such as the connection 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); 

问候,

尚卡尔

推荐答案

使用了 HttpConnectionParams 你的 DefaultHttpClient

final HttpParams httpParameters = yourHttpClient.getParams();

HttpConnectionParams.setConnectionTimeout(httpParameters, connectionTimeOutSec * 1000);
HttpConnectionParams.setSoTimeout        (httpParameters, socketTimeoutSec * 1000);