要使用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須要修改爲本身所須要的地址