[Socket]利用Android下的HttpClient發送GET && POST請求

來源:互聯網
上載者:User

上一篇寫的是利用Jakarta下的org.apache.commons.的HttpClient發送GET和POST請求,但是Android的jar包已經整合了org.apache.http.client.HttpClient類,所以本文主要介紹Android下的HttpClient發送GET和POST請求:

 

以下為轉帖:

本文將介紹Android SDK整合的Apache HttpClient模組。要注意的是,這裡的Apache HttpClient模組是HttpClient 4.0(org.apache.http.*),而不是Jakarta Commons HttpClient 3.x(org.apache.commons.httpclient.*)。

在HttpClient模組中用到了兩個重要的類:HttpGet和HttpPost。這兩個類分別用來提交HTTP GET和HTTP POST請求。為了測試本節的例子,需要先編寫一個Servlet程式,用來接收HTTP GET和

HTTP POST請求。讀者也可以使用其他服務端的資源來測試本節的例子。

假設192.168.17.81是原生IP,用戶端可以通過如下的URL來訪問服務端的資源:

http://192.168.17.81:8080/querybooks/QueryServlet?bookname=開發

在這裡bookname是QueryServlet的請求參數,表示圖書名,通過該參數來查詢圖書資訊。

現在我們要通過HttpGet和HttpPost類向QueryServlet提交請求資訊,並將返回結果顯示在TextView組件中。

無論是使用HttpGet,還是使用HttpPost,都必須通過如下3步來訪問HTTP資源。

1.建立HttpGet或HttpPost對象,將要請求的URL通過構造方法傳入HttpGet或HttpPost對象。

2.使用DefaultHttpClient類的execute方法發送HTTP GET或HTTP POST請求,並返回HttpResponse對象。

3.通過HttpResponse介面的getEntity方法返迴響應資訊,並進行相應的處理。

如果使用HttpPost方法提交HTTP POST請求,還需要使用HttpPost類的setEntity方法佈建要求參數。

本例使用了兩個按鈕來分別提交HTTP GET和HTTP POST請求,並從EditText組件中獲得請求參數(bookname)值,最後將返回結果顯示在TextView組件中。兩個按鈕共用一個onClick事件方法,

代碼如下:

public void onClick(View view){    //讀者需要將本例中的IP換成自己機器的IP      String url = "http://192.168.17.81:8080/querybooks/QueryServlet";      TextView tvQueryResult = (TextView) findViewById(R.id.tvQueryResult);      EditText etBookName = (EditText) findViewById(R.id.etBookName);      HttpResponse httpResponse = null;      try{  switch (view.getId()){              //  提交HTTP GET請求              case R.id.btnGetQuery:                  //  向url添加請求參數                  url += "?bookname=" + etBookName.getText().toString();                  //  第1步:建立HttpGet對象                  HttpGet httpGet = new HttpGet(url);                  //  第2步:使用execute方法發送HTTP GET請求,並返回HttpResponse對象                  httpResponse = new DefaultHttpClient().execute(httpGet);                  //  判斷請求響應狀態代碼,狀態代碼為200表示服務端成功響應了用戶端的請求                  if (httpResponse.getStatusLine().getStatusCode() == 200){                  //  第3步:使用getEntity方法獲得返回結果                  String result = EntityUtils.toString(httpResponse.getEntity());                //  去掉返回結果中的"\r"字元,否則會在結果字串後面顯示一個小方格                      tvQueryResult.setText(result.replaceAll("\r", ""));                  }                  break;                  //  提交HTTP POST請求              case R.id.btnPostQuery:                  //  第1步:建立HttpPost對象                  HttpPost httpPost = new HttpPost(url);                  //  設定HTTP POST請求參數必須用NameValuePair對象                  List<NameValuePair> params = new ArrayList<NameValuePair>();                  params.add(new BasicNameValuePair("bookname", etBookName.getText(). toString()));                  //  設定HTTP POST請求參數                  httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));                  //  第2步:使用execute方法發送HTTP POST請求,並返回HttpResponse對象                  httpResponse = new DefaultHttpClient().execute(httpPost);                  if (httpResponse.getStatusLine().getStatusCode() == 200){                      //  第3步:使用getEntity方法獲得返回結果                      String result = EntityUtils.toString(httpResponse.getEntity());                      //  去掉返回結果中的"\r"字元,否則會在結果字串後面顯示一個小方格                      tvQueryResult.setText(result.replaceAll("\r", ""));                  }                  break;          }      }catch (Exception e){          tvQueryResult.setText(e.getMessage());    }  

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.