码迷,mamicode.com
首页 > 移动开发 > 详细

Android发送数据到web服务器4种方式

时间:2014-12-22 10:52:16      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

  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种方式

标签:

原文地址:http://www.cnblogs.com/xiaoblog/p/4177573.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!