日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

android http get post

 昵稱31502522 2016-03-14

要使用HttpClient,需要了解一些類:

1、ClientConnectionManager接口:該接口是客戶端連接管理器接口,主要提供以下幾個抽象方法:

ClientConnectionManager(關(guān)閉所有無效超時的連接)、closeIdleConnection(關(guān)閉空閑的連接)、releaseConnection(釋放一個連接)、requestConnection(請求一個新的連接)、shutdown(關(guān)閉管理器并且釋放資源)

2、DefaultHttpClient:一個默認(rèn)的Http客戶端,我們可以使用它來創(chuàng)建一個Http連接,代碼如下:

[java] view plain copy
  1. HttpClient httpClient = new DefaultHttpClient();  

3、HttpResponse:是一個Http連接相應(yīng),當(dāng)執(zhí)行一個Http連接后,就會返回一個HttpResponse,可以通過HttpResponse獲得一些響應(yīng)信息。下面是一個請求Http連接并且獲得該請求是否成功的代碼:

[java] view plain copy
  1. HttpResponse httpResponse = httpClient.execute(httpGet);  
  2.             if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){  
  3.                 //連接成功  
  4.             }  

通過上面幾個類的連接,下面將分別使用Get和Post方式請求一個網(wǎng)頁。

其中有兩個資源文件,兩個jsp的內(nèi)容分別如下:
http.jsp
[html] view plain copy
  1. <html>  
  2.   <head>   
  3.     <title>My JSP 'index.jsp' starting page</title>  
  4.   </head>  
  5.   <body>  
  6.     <%  
  7.         out.println("<h1>HTTP TEST<br/>http test</h1>");  
  8.      %>  
  9.   </body>  
  10. </html>  
httpGet.jsp
[html] view plain copy
  1. <html>  
  2.   <head>  
  3.     <title>My JSP 'index.jsp' starting page</title>  
  4.   </head>   
  5.   <body>  
  6.     <%  
  7.         String type = request.getParameter("par");  
  8.         String result = new String(type.getBytes("iso-8859-1"),"gb2312");  
  9.         out.println("<h1>parameters:"+result+"</h1>");  
  10.      %>  
  11.   </body>  
  12. </html>  

先看看HttpClient中如何使用Get方式獲取數(shù)據(jù),這里需要使用HttpGet來構(gòu)建一個Get方式的Http請求,然后通過HttpClient來執(zhí)行這個請求,HttpResponse在收到這個請求后給出響應(yīng),最后通過“httpResponse.getStatusLine().getStatusCode()”來判斷請求是否成功,并且處理,具體代碼如下:

[java] view plain copy
  1. public class GetActivity extends Activity {  
  2.     @Override  
  3.     protected void onCreate(Bundle savedInstanceState) {  
  4.         // TODO Auto-generated method stub  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.http);  
  7.         TextView textView = (TextView)findViewById(R.id.text);  
  8.           
  9.         String httpUrl = "http://59.64.158.106:8080/test/http.jsp";  
  10.         //HttpGet對象  
  11.         HttpGet httpGet = new HttpGet(httpUrl);  
  12.         try{  
  13.             //取得HttpClient對象  
  14.             HttpClient httpClient = new DefaultHttpClient();  
  15.             //請求HttpClient,取得HttpResponse  
  16.             HttpResponse httpResponse = httpClient.execute(httpGet);  
  17.             //請求成功  
  18.             if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){  
  19.                 //取得返回的字符串  
  20.                 String strResult = EntityUtils.toString(httpResponse.getEntity());  
  21.                 textView.setText(strResult);  
  22.             }else{  
  23.                 textView.setText("請求錯誤");  
  24.             }  
  25.         }catch (ClientProtocolException e) {  
  26.             // TODO: handle exception  
  27.             Log.e("GetActivity""ClientProtocolException");  
  28.             e.printStackTrace();  
  29.         }catch (IOException e) {  
  30.             // TODO: handle exception  
  31.             Log.e("GetActivity""IOException");  
  32.             e.printStackTrace();  
  33.         }  
  34.     }  
  35. }  

Post方法則比Get方法稍微復(fù)雜一點。首先使用HttpPost來構(gòu)建一個Post方式的請求,然后需要使用NameValuePair來保存要傳遞的參數(shù),還需要設(shè)置所使用的字符集,最后就和Get方式一樣通過HttpClient來請求這個鏈接,返回響應(yīng)并且處理,下面是一個例子:

[java] view plain copy
  1. public class PostActivity extends Activity {  
  2.     @Override  
  3.     protected void onCreate(Bundle savedInstanceState) {  
  4.         super.onCreate(savedInstanceState);  
  5.         setContentView(R.layout.http);  
  6.         TextView textView = (TextView)findViewById(R.id.text);  
  7.         //http地址  
  8.         String httpUrl = "http://59.64.158.106:8080/test/httpGet.jsp";  
  9.         //httpPost連接對象  
  10.         HttpPost httpPost = new HttpPost(httpUrl);  
  11.         //使用NameValuePair來保存要傳遞的post闡述  
  12.         List<NameValuePair> params = new ArrayList<NameValuePair>();  
  13.         //添加要傳遞的參數(shù)  
  14.         params.add(new BasicNameValuePair("par""HttpClient_android_post"));  
  15.         try{  
  16.             //設(shè)置字符集  
  17.             HttpEntity httpEntity = new UrlEncodedFormEntity(params, "gb2312");  
  18.             httpPost.setEntity(httpEntity);  
  19.             //取得默認(rèn)的HttpClient  
  20.             HttpClient httpClient = new DefaultHttpClient();  
  21.             //取得HttpResponse  
  22.             HttpResponse httpResponse = httpClient.execute(httpPost);  
  23.             //HttpStatus.SC_OK)表示連接成功  
  24.             if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){  
  25.                 //取得返回的字符串  
  26.                 String result = EntityUtils.toString(httpResponse.getEntity());  
  27.                 textView.setText(result);  
  28.             }else{  
  29.                 textView.setText("請求錯誤");  
  30.             }  
  31.         }catch (ClientProtocolException e) {  
  32.             Log.e("PostActivity""ClientProtocolException");  
  33.             e.printStackTrace();  
  34.         }catch (IOException e) {  
  35.             Log.e("PostActivity""IOException");  
  36.             e.printStackTrace();  
  37.         }  
  38.     }  
  39. }  

注意:代碼中的url地址中的ip:127.0.0.1需要修改成自己所需要的地址

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多