要使用HttpClient,须要了解一些类:html
一、ClientConnectionManager接口:该接口是客户端链接管理器接口,主要提供如下几个抽象方法:java
ClientConnectionManager(关闭全部无效超时的链接)、closeIdleConnection(关闭空闲的链接)、releaseConnection(释放一个链接)、requestConnection(请求一个新的链接)、shutdown(关闭管理器而且释放资源)android
二、DefaultHttpClient:一个默认的Http客户端,咱们可使用它来建立一个Http链接,代码以下:web
HttpClient httpClient = new DefaultHttpClient();
三、HttpResponse:是一个Http链接相应,当执行一个Http链接后,就会返回一个HttpResponse,能够经过HttpResponse得到一些响应信息。下面是一个请求Http链接而且得到该请求是否成功的代码:jsp
HttpResponse httpResponse = httpClient.execute(httpGet); if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ //链接成功 }
经过上面几个类的链接,下面将分别使用Get和Post方式请求一个网页。ide
其中有两个资源文件,两个jsp的内容分别以下:post
http.jspurl
<html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body> <% out.println("<h1>HTTP TEST<br/>http test</h1>"); %> </body> </html>
httpGet.jspspa
<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()”来判断请求是否成功,而且处理,具体代码以下:code
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 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须要修改为本身所须要的地址