本文介绍了超时DefaultHttpClient级的Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经创建了一个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);
这篇关于超时DefaultHttpClient级的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!