要使用HttpClient,需要了解一些類:
1、ClientConnectionManager接口:該接口是客戶端連接管理器接口,主要提供以下幾個抽象方法:
ClientConnectionManager(關(guān)閉所有無效超時的連接)、closeIdleConnection(關(guān)閉空閑的連接)、releaseConnection(釋放一個連接)、requestConnection(請求一個新的連接)、shutdown(關(guān)閉管理器并且釋放資源)
2、DefaultHttpClient:一個默認(rèn)的Http客戶端,我們可以使用它來創(chuàng)建一個Http連接,代碼如下:
- HttpClient httpClient = new DefaultHttpClient();
3、HttpResponse:是一個Http連接相應(yīng),當(dāng)執(zhí)行一個Http連接后,就會返回一個HttpResponse,可以通過HttpResponse獲得一些響應(yīng)信息。下面是一個請求Http連接并且獲得該請求是否成功的代碼:
- HttpResponse httpResponse = httpClient.execute(httpGet);
- if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
-
- }
通過上面幾個類的連接,下面將分別使用Get和Post方式請求一個網(wǎng)頁。
其中有兩個資源文件,兩個jsp的內(nèi)容分別如下:
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>
httpGet.jsp
- <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方式獲取數(shù)據(jù),這里需要使用HttpGet來構(gòu)建一個Get方式的Http請求,然后通過HttpClient來執(zhí)行這個請求,HttpResponse在收到這個請求后給出響應(yīng),最后通過“httpResponse.getStatusLine().getStatusCode()”來判斷請求是否成功,并且處理,具體代碼如下:
- public class GetActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
-
- 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 = new HttpGet(httpUrl);
- try{
-
- HttpClient httpClient = new DefaultHttpClient();
-
- 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) {
-
- Log.e("GetActivity", "ClientProtocolException");
- e.printStackTrace();
- }catch (IOException e) {
-
- Log.e("GetActivity", "IOException");
- e.printStackTrace();
- }
- }
- }
Post方法則比Get方法稍微復(fù)雜一點。首先使用HttpPost來構(gòu)建一個Post方式的請求,然后需要使用NameValuePair來保存要傳遞的參數(shù),還需要設(shè)置所使用的字符集,最后就和Get方式一樣通過HttpClient來請求這個鏈接,返回響應(yīng)并且處理,下面是一個例子:
- 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);
-
- String httpUrl = "http://59.64.158.106:8080/test/httpGet.jsp";
-
- HttpPost httpPost = new HttpPost(httpUrl);
-
- 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 = new DefaultHttpClient();
-
- HttpResponse httpResponse = httpClient.execute(httpPost);
-
- 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需要修改成自己所需要的地址