返回
顶部

修改密码

首页 > 教程 > 网络通信 > HTTP > 正文
Android Http请求

+1

-1

收藏

+1

-1

点赞0

评论0

比较简单直接贴代码了。 package jftt.test; import java.io.IOException; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProt…
比较简单直接贴代码了。
 

 
  1. package jftt.test;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.List;  
  5.   
  6. import org.apache.http.HttpResponse;  
  7. import org.apache.http.HttpStatus;  
  8. import org.apache.http.NameValuePair;  
  9. import org.apache.http.client.ClientProtocolException;  
  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.params.BasicHttpParams;  
  16. import org.apache.http.params.HttpConnectionParams;  
  17. import org.apache.http.params.HttpParams;  
  18.   
  19. import android.util.Log;  
  20.   
  21. public class HttpRequest {  
  22.     /** 
  23.      *Post请求 
  24.      */  
  25.     public void doPost(String url , List<NameValuePair> nameValuePairs){  
  26.         //新建HttpClient对象    
  27.         HttpClient httpclient = new DefaultHttpClient();  
  28.         //创建POST连接  
  29.         HttpPost httppost = new HttpPost(url);  
  30.         try {  
  31. //          //使用PSOT方式,必须用NameValuePair数组传递参数  
  32. //          List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
  33. //          nameValuePairs.add(new BasicNameValuePair("id", "12345"));  
  34. //          nameValuePairs.add(new BasicNameValuePair("stringdata","hps is Cool!"));  
  35.             httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));  
  36.             HttpResponse response = httpclient.execute(httppost);  
  37.         } catch (ClientProtocolException e) {  
  38.             e.printStackTrace();  
  39.         } catch (IOException e) {  
  40.             e.printStackTrace();  
  41.         }  
  42.     }  
  43.       
  44.     /** 
  45.      *Get请求 
  46.      */  
  47.     public void doGet(String url){  
  48.         HttpParams httpParams = new BasicHttpParams();  
  49.         HttpConnectionParams.setConnectionTimeout(httpParams,30000);    
  50.         HttpConnectionParams.setSoTimeout(httpParams, 30000);    
  51.               
  52.         HttpClient httpClient = new DefaultHttpClient(httpParams);    
  53.         // GET  
  54.         HttpGet httpGet = new HttpGet(url);  
  55.         try {  
  56.             HttpResponse response = httpClient.execute(httpGet);  
  57.             if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK){  
  58.                 Log.i("GET", "Bad Request!");  
  59.             }  
  60.         } catch (IOException e) {  
  61.             e.printStackTrace();  
  62.         }  
  63.   
  64.     }  
  65. }  

 
需要主要的是:
1.    使用POST方式时,传递参数必须使用NameValuePair数组
2.    使用GET方式时,通过URL传递参数,注意写法
3.      通过setEntity方法来发送HTTP请求
4.      通过DefaultHttpClient 的 execute方法来获取HttpResponse
5. 通过getEntity()从Response中获取内容
 

 
  1. String content = EntityUtils.toString(response.getEntity());  



特别说明:
对于WCF的json服务,请求时如下:

 
  1.     /**  
  2.      *Post请求  
  3.      * @throws IOException  
  4.      * @throws ClientProtocolException  
  5.      */    
  6.     public static String doPost(String url , List<NameValuePair> nameValuePairs) throws ClientProtocolException, IOException{   
  7.         String result = null;  
  8.         //新建HttpClient对象      
  9.         HttpClient httpclient = new DefaultHttpClient();    
  10.         //创建POST连接    
  11.         HttpPost httppost = new HttpPost(url);    
  12.         httppost.setHeader("content-type", "application/json");  
  13.         try {  
  14.             if(nameValuePairs != null) {  
  15.                 StringEntity entity = new StringEntity("这里是JSON数据,如{"id":"12","name":"xiaoming"}", "utf-8");  
  16.                 entity.setContentType("application/json");  
  17.                 entity.setContentEncoding("utf-8");  
  18.                 httppost.setEntity(entity);  
  19.             }  
  20.                 
  21. //          if(nameValuePairs != null) {  
  22. //              httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));    
  23. //          }  
  24.             HttpResponse response = httpclient.execute(httppost);    
  25.             if (response.getStatusLine().getStatusCode() != 404)  
  26.             {  
  27.                     result = EntityUtils.toString(response.getEntity());  
  28.                     Logger.d(TAG, "Response: " + result);  
  29.             }  
  30.         } finally {  
  31.               
  32.         }  
  33.         return result;  
  34.     }  

其实就相当于传数据流的方式。

 

原文链接:http://blog.csdn.net/feng88724/article/details/6170021

扫一扫在手机打开

评论
已有0条评论
0/150
提交
热门评论
相关推荐
今日要闻
换一批
热点排行