android中使用httpclient提交表单

来源:互联网 发布:独生子女政策 知乎 编辑:程序博客网 时间:2024/05/21 06:35

在android开发中如果我们需要和服务器端交互的时候已经使用apache client项目进行表单的模拟提交。通过返回json对象进而来达到数据的提交与取回,同时又能保证session的有效性以及系统的安全性,这里我封装了一下httpclient的get和post方式来提交数据


[java] view plaincopy
  1. /** 
  2.  *  
  3.  */  
  4. package com.jiuchongju.util;  
  5.   
  6. import java.util.ArrayList;  
  7. import java.util.List;  
  8. import java.util.Map;  
  9.   
  10. import org.apache.http.HttpResponse;  
  11. import org.apache.http.NameValuePair;  
  12. import org.apache.http.client.HttpClient;  
  13. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  14. import org.apache.http.client.methods.HttpGet;  
  15. import org.apache.http.client.methods.HttpPost;  
  16. import org.apache.http.impl.client.DefaultHttpClient;  
  17. import org.apache.http.message.BasicNameValuePair;  
  18. import org.apache.http.protocol.HTTP;  
  19. import org.apache.http.util.EntityUtils;  
  20. /** 
  21.  * Description: 
  22.  
  23.  * @version  1.0 
  24.  */  
  25. public class HttpUtil  
  26. {  
  27.     // 创建HttpClient对象  
  28.     public static HttpClient httpClient ;  
  29.     public static final String BASE_URL =   
  30.         "http://www.xxx.com/projectname/name_baidu!jsonstring";  
  31.     /** 
  32.      *  
  33.      * @param url 发送请求的URL 
  34.      * @return 服务器响应字符串 
  35.      * @throws Exception 
  36.      */  
  37.     public static String getRequest(String url)  
  38.         throws Exception  
  39.     {  
  40.         httpClient= new DefaultHttpClient();  
  41.           
  42.         try{  
  43.             // 创建HttpGet对象。  
  44.             HttpGet get = new HttpGet(url);  
  45.             // 发送GET请求  
  46.             HttpResponse httpResponse = httpClient.execute(get);  
  47.             // 如果服务器成功地返回响应  
  48.             if (httpResponse.getStatusLine()  
  49.                 .getStatusCode() == 200)  
  50.             {  
  51.                 // 获取服务器响应字符串  
  52.                 String result = EntityUtils  
  53.                     .toString(httpResponse.getEntity());  
  54.                 return result;  
  55.             }  
  56.         }catch(Exception e){  
  57.               
  58.             e.printStackTrace();  
  59.             return "获取数据失败!";  
  60.         }finally{  
  61.             httpClient.getConnectionManager().shutdown();  
  62.         }  
  63.   
  64.         return null;  
  65.     }  
  66.   
  67.     /** 
  68.      *  
  69.      * @param url 发送请求的URL 
  70.      * @param params 请求参数 
  71.      * @return 服务器响应字符串 
  72.      * @throws Exception 
  73.      */  
  74.     public static String postRequest(String url  
  75.         , Map<String ,String> rawParams)  
  76.     {  
  77.         httpClient= new DefaultHttpClient();  
  78.         try{  
  79.             // 创建HttpPost对象。  
  80.             HttpPost post = new HttpPost(url);  
  81.             // 如果传递参数个数比较多的话可以对传递的参数进行封装  
  82.             List<NameValuePair> params = new ArrayList<NameValuePair>();  
  83.             for(String key : rawParams.keySet())  
  84.             {  
  85.                 //封装请求参数  
  86.                 params.add(new BasicNameValuePair(key , rawParams.get(key)));  
  87.             }  
  88.             // 设置请求参数  
  89.             post.setEntity(new UrlEncodedFormEntity(  
  90.                 params,HTTP.UTF_8));  
  91.             // 发送POST请求  
  92.             HttpResponse httpResponse = httpClient.execute(post);  
  93.             // 如果服务器成功地返回响应  
  94.             if (httpResponse.getStatusLine()  
  95.                 .getStatusCode() == 200)  
  96.             {  
  97.                 // 获取服务器响应字符串  
  98.                 String result = EntityUtils  
  99.                     .toString(httpResponse.getEntity());  
  100.                 return result;  
  101.             }  
  102.         }catch(Exception e){  
  103.             e.printStackTrace();  
  104.         }finally{  
  105.             httpClient.getConnectionManager().shutdown();  
  106.         }  
  107.         return null;  
  108.     }  
  109. }  


需要说明的是,这个一般不能直接在主线程来做这个动作,这样会因为网络问题造成主线程假死。手机客户端的UI卡掉

一般我都是异步进行数据的调用


原创粉丝点击