我创建了一个 Android 应用程序,我在其中连接到远程服务器 php 文件以检索一些信息。下面是代码。
这里我想为连接添加超时,例如连接将在 5 秒后超时。
知道如何做到这一点。
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);
问候,
香卡
最佳答案
使用 DefaultHttpClient
的 HttpConnectionParams
::
final HttpParams httpParameters = yourHttpClient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, connectionTimeOutSec * 1000);
HttpConnectionParams.setSoTimeout (httpParameters, socketTimeoutSec * 1000);
关于android - DefaultHttpClient 类 Android 中的超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7229189/