android - DefaultHttpClient 类 Android 中的超时

标签 android

我创建了一个 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); 

问候,

香卡

最佳答案

使用 DefaultHttpClientHttpConnectionParams::

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/

相关文章:

android - com.android.builder.merge.DuplicateRelativeFileException:使用操作系统独立路径 'META-INF/INDEX.LIST'找到了多个文件

java - 即使 PlayerView 已暂停,Exoplayer 也会播放音频

Android APK 在 google play 中不可见

java - 我无法在 ListView 中将文本居中

android - Google API 中的server-key、android-key、browser-key 和iOS key 有什么区别?

android - Active Directory B2C 可以使用 Native Facebook App 而不是 webview 吗?

Android:如何使用 LocalBroadcastManager

java - Android 中的膨胀和腐 eclipse

java - 如何正确调用列表项子项的onClick方法?

android - @SuppressLit(( {"ParseError","ParseError"})