安卓post get提交表单类

来源:互联网 发布:剑灵高冷正太捏脸数据 编辑:程序博客网 时间:2024/06/06 09:41


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