httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 6*1000);//设置请求超时
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 6*1000);//设置读取超时
转自:http://blog.csdn.net/llbupt/article/details/7362416
要使用HttpClient,需要了解一些类:
1、ClientConnectionManager接口:该接口是客户端连接管理器接口,主要提供以下几个抽象方法:
ClientConnectionManager(关闭所有无效超时的连接)、closeIdleConnection(关闭空闲的连接)、releaseConnection(释放一个连接)、requestConnection(请求一个新的连接)、shutdown(关闭管理器并且释放资源)
2、DefaultHttpClient:一个默认的Http客户端,我们可以使用它来创建一个Http连接,代码如下:
- HttpClient httpClient = new DefaultHttpClient();
3、HttpResponse:是一个Http连接相应,当执行一个Http连接后,就会返回一个HttpResponse,可以通过HttpResponse获得一些响应信息。下面是一个请求Http连接并且获得该请求是否成功的代码:
- HttpResponse httpResponse = httpClient.execute(httpGet);
- if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
- //连接成功
- }
</pre><pre name="code" class="java">httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 6*1000);//设置请求超时
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 6*1000);//设置读取超时
HttpResponse httpResponse = httpClient.execute(httpPost);//获得一些响应信息,已经连接完成。设置超时需要在连接完成之前设置
通过上面几个类的连接,下面将分别使用Get和Post方式请求一个网页。
其中有两个资源文件,两个jsp的内容分别如下:
http.jsp
- <html>
- <head>
- <title>My JSP 'index.jsp' starting page</title>
- </head>
- <body>
- <%
- out.println("<h1>HTTP TEST<br/>http test</h1>");
- %>
- </body>
- </html>
- <html>
- <head>
- <title>My JSP 'index.jsp' starting page</title>
- </head>
- <body>
- <%
- String type = request.getParameter("par");
- String result = new String(type.getBytes("iso-8859-1"),"gb2312");
- out.println("<h1>parameters:"+result+"</h1>");
- %>
- </body>
- </html>
先看看HttpClient中如何使用Get方式获取数据,这里需要使用HttpGet来构建一个Get方式的Http请求,然后通过HttpClient来执行这个请求,HttpResponse在收到这个请求后给出响应,最后通过“httpResponse.getStatusLine().getStatusCode()”来判断请求是否成功,并且处理,具体代码如下:
- public class GetActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.http);
- TextView textView = (TextView)findViewById(R.id.text);
- String httpUrl = "http://59.64.158.106:8080/test/http.jsp";
- //HttpGet对象
- HttpGet httpGet = new HttpGet(httpUrl);
- try{
- //取得HttpClient对象
- HttpClient httpClient = new DefaultHttpClient();
- //请求HttpClient,取得HttpResponse
- httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 6*1000);//设置读取超时
- httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 6*1000);//设置请求超时
- HttpResponse httpResponse = httpClient.execute(httpGet);
- //请求成功
- if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
- //取得返回的字符串
- String strResult = EntityUtils.toString(httpResponse.getEntity());
- textView.setText(strResult);
- }else{
- textView.setText("请求错误");
- }
- }catch (ClientProtocolException e) {
- // TODO: handle exception
- Log.e("GetActivity", "ClientProtocolException");
- e.printStackTrace();
- }catch (IOException e) {
- // TODO: handle exception
- Log.e("GetActivity", "IOException");
- e.printStackTrace();
- }
- }
- }
Post方法则比Get方法稍微复杂一点。首先使用HttpPost来构建一个Post方式的请求,然后需要使用NameValuePair来保存要传递的参数,还需要设置所使用的字符集,最后就和Get方式一样通过HttpClient来请求这个链接,返回响应并且处理,下面是一个例子:
- public class PostActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.http);
- TextView textView = (TextView)findViewById(R.id.text);
- //http地址
- String httpUrl = "http://59.64.158.106:8080/test/httpGet.jsp";
- //httpPost连接对象
- HttpPost httpPost = new HttpPost(httpUrl);
- //使用NameValuePair来保存要传递的post阐述
- List<NameValuePair> params = new ArrayList<NameValuePair>();
- //添加要传递的参数
- params.add(new BasicNameValuePair("par", "HttpClient_android_post"));
- try{
- //设置字符集
- HttpEntity httpEntity = new UrlEncodedFormEntity(params, "gb2312");
- httpPost.setEntity(httpEntity);
- //取得默认的HttpClient
- HttpClient httpClient = new DefaultHttpClient();
- //取得HttpResponse
- HttpResponse httpResponse = httpClient.execute(httpPost);
- //HttpStatus.SC_OK)表示连接成功
- if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
- //取得返回的字符串
- String result = EntityUtils.toString(httpResponse.getEntity());
- textView.setText(result);
- }else{
- textView.setText("请求错误");
- }
- }catch (ClientProtocolException e) {
- Log.e("PostActivity", "ClientProtocolException");
- e.printStackTrace();
- }catch (IOException e) {
- Log.e("PostActivity", "IOException");
- e.printStackTrace();
- }
- }
- }
注意:代码中的url地址中的ip:127.0.0.1需要修改成自己所需要的地址