前言
歡迎大家我分享和推薦好用的代碼段~~
聲明
歡迎轉載,但請保留文章原始出處:
CSDN:http://www.csdn.net
雨季o莫憂離:http://blog.csdn.net/luckkof
正文
要使用HttpClient,需要了解一些類:
1、ClientConnectionManager接口:該接口是客戶端連接管理器接口,主要提供以下幾個抽象方法:
ClientConnectionManager(關閉所有無效超時的連接)、closeIdleConnection(關閉空閒的連接)、releaseConnection(釋放一個連接)、requestConnection(請求一個新的連接)、shutdown(關閉管理器並且釋放資源)
2、DefaultHttpClient:一個默認的Http客戶端,我們可以使用它來創建一個Http連接,代碼如下:
- HttpClient httpClient = new DefaultHttpClient();
- HttpResponse httpResponse = httpClient.execute(httpGet);
- if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
- //連接成功
- }
通過上面幾個類的連接,下面將分別使用Get和Post方式請求一個網頁。
- <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
- 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();
- }
- }
- }