Android發送資料到web伺服器4種方式

來源:互聯網
上載者:User

標籤:

  1. /**  
  2.  * Android中向web伺服器提交資料的兩種方式四種方法  
  3.  */ 
  4. public class SubmitDataByHttpClientAndOrdinaryWay {  
  5.      
  6.     /**  
  7.      * 使用get請求以普通方式提交資料  
  8.      * @param map 傳遞進來的資料,以map的形式進行了封裝  
  9.      * @param path 要求伺服器servlet的地址  
  10.      * @return 返回的boolean類型的參數  
  11.      * @throws Exception  
  12.      */ 
  13.     public Boolean submitDataByDoGet(Map<String, String> map, String path) throws Exception {  
  14.         // 拼湊出請求地址  
  15.         StringBuilder sb = new StringBuilder(path);  
  16.         sb.append("?");  
  17.         for (Map.Entry<String, String> entry : map.entrySet()) {  
  18.             sb.append(entry.getKey()).append("=").append(entry.getValue());  
  19.             sb.append("&");  
  20.         }  
  21.         sb.deleteCharAt(sb.length() - 1);  
  22.         String str = sb.toString();  
  23.         System.out.println(str);  
  24.         URL Url = new URL(str);  
  25.         HttpURLConnection HttpConn = (HttpURLConnection) Url.openConnection();  
  26.         HttpConn.setRequestMethod("GET");  
  27.         HttpConn.setReadTimeout(5000);  
  28.         if (HttpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {  
  29.             return true;  
  30.         }  
  31.         return false;  
  32.     }  
  33.      
  34.     /**  
  35.      * 普通方式的DoPost請求提交資料  
  36.      * @param map 傳遞進來的資料,以map的形式進行了封裝  
  37.      * @param path 要求伺服器servlet的地址  
  38.      * @return 返回的boolean類型的參數  
  39.      * @throws Exception  
  40.      */ 
  41.     public Boolean submitDataByDoPost(Map<String, String> map, String path) throws Exception {  
  42.         // 注意Post地址中是不帶參數的,所以newURL的時候要注意不能加上後面的參數  
  43.         URL Url = new URL(path);  
  44.         // Post方式提交的時候參數和URL是分開提交的,參數形式是這樣子的:name=y&age=6  
  45.         StringBuilder sb = new StringBuilder();  
  46.         // sb.append("?");  
  47.         for (Map.Entry<String, String> entry : map.entrySet()) {  
  48.             sb.append(entry.getKey()).append("=").append(entry.getValue());  
  49.             sb.append("&");  
  50.         }  
  51.         sb.deleteCharAt(sb.length() - 1);  
  52.         String str = sb.toString();  
  53.      
  54.         HttpURLConnection HttpConn = (HttpURLConnection) Url.openConnection();  
  55.         HttpConn.setRequestMethod("POST");  
  56.         HttpConn.setReadTimeout(5000);  
  57.         HttpConn.setDoOutput(true);  
  58.         HttpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");  
  59.         HttpConn.setRequestProperty("Content-Length", String.valueOf(str.getBytes().length));  
  60.         OutputStream os = HttpConn.getOutputStream();  
  61.         os.write(str.getBytes());  
  62.         if (HttpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {  
  63.             return true;  
  64.         }  
  65.         return false;  
  66.     }  
  67.      
  68.     /**  
  69.      * 以HttpClient的DoGet方式向伺服器發送請資料  
  70.      * @param map 傳遞進來的資料,以map的形式進行了封裝  
  71.      * @param path 要求伺服器servlet的地址  
  72.      * @return 返回的boolean類型的參數  
  73.      * @throws Exception  
  74.      */ 
  75.     public Boolean submitDataByHttpClientDoGet(Map<String, String> map, String path) throws Exception {  
  76.         HttpClient hc = new DefaultHttpClient();  
  77.         // 請求路徑  
  78.         StringBuilder sb = new StringBuilder(path);  
  79.         sb.append("?");  
  80.         for (Map.Entry<String, String> entry : map.entrySet()) {  
  81.             sb.append(entry.getKey()).append("=").append(entry.getValue());  
  82.             sb.append("&");  
  83.         }  
  84.         sb.deleteCharAt(sb.length() - 1);  
  85.         String str = sb.toString();  
  86.         System.out.println(str);  
  87.         HttpGet request = new HttpGet(sb.toString());  
  88.      
  89.         HttpResponse response = hc.execute(request);  
  90.         if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {  
  91.             return true;  
  92.         }  
  93.         return false;  
  94.     }  
  95.          
  96.     /**  
  97.      * 以HttpClient的DoPost方式提交資料到伺服器  
  98.      * @param map 傳遞進來的資料,以map的形式進行了封裝  
  99.      * @param path 要求伺服器servlet的地址  
  100.      * @return 返回的boolean類型的參數  
  101.      * @throws Exception  
  102.      */ 
  103.     public Boolean submintDataByHttpClientDoPost(Map<String, String> map, String path) throws Exception {  
  104.         // 1. 獲得一個相當於瀏覽器對象HttpClient,使用這個介面的實作類別來建立對象,DefaultHttpClient  
  105.         HttpClient hc = new DefaultHttpClient();  
  106.         // DoPost方式請求的時候佈建要求,關鍵是路徑  
  107.         HttpPost request = new HttpPost(path);  
  108.         // 2. 為請求佈建要求參數,也即是將要上傳到web伺服器上的參數  
  109.         List<NameValuePair> parameters = new ArrayList<NameValuePair>();  
  110.         for (Map.Entry<String, String> entry : map.entrySet()) {  
  111.             NameValuePair nameValuePairs = new BasicNameValuePair(entry.getKey(), entry.getValue());  
  112.             parameters.add(nameValuePairs);  
  113.         }  
  114.         // 請求實體HttpEntity也是一個介面,我們用它的實作類別UrlEncodedFormEntity來建立對象,注意後面一個String類型的參數是用來指定編碼的  
  115.         HttpEntity entity = new UrlEncodedFormEntity(parameters, "UTF-8");  
  116.         request.setEntity(entity);  
  117.         // 3. 執行請求  
  118.         HttpResponse response = hc.execute(request);  
  119.         // 4. 通過返回碼來判斷請求成功與否  
  120.         if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {  
  121.             return true;  
  122.         }  
  123.         return false;  
  124.     }  
  125. }

 

原文出自 http://luecsc.blog.51cto.com/2219432/1111923

Android發送資料到web伺服器4種方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.