自己封装的调用WCF Rest的函数

package www.Framework.Android;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
//新增WCFREST处理
public class HttpRestClientHelp {
    
    
    //获取列表,封装基于HttpGet
     public  JSONObject      getRestList(String uri,List<NameValuePair> paramsList ) throws JSONException, IOException
        {
            //创建一个http客户端  
           // HttpClient client=new DefaultHttpClient();  
            //创建一个GET请求  
        
            HttpGet httpGet=new HttpGet(uri);  
            httpGet.setHeader("Accept", "application/json");
            httpGet.setHeader("Content-type", "application/json");
             //向服务器发送请求并获取服务器返回的结果  
            HttpResponse response = null;
            try {
                response= new DefaultHttpClient().execute(httpGet);  
                //response = client.execute(httpGet);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  
              if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
               {
                  System.out.println("OK");
               }
              else
              {
                  
                String status=  response.getStatusLine().getStatusCode()+" ";
                 System.out.println(status);
              }
            
            
            //返回的结果可能放到InputStream,http Header中等。
                InputStream inputStream=null;
            try {
                 inputStream=response.getEntity().getContent();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  
        //    Header[] headers=response.getAllHeaders();  
//            System.out.println(InputStream.class.toString());
//            System.out.println(headers.length+"|"+headers.toString());
             
            char[] buffer = new char[(int)response.getEntity().getContentLength()];

            InputStreamReader reader = new InputStreamReader(inputStream);
            reader.read(buffer);
            inputStream.close();

            JSONObject entitys = new JSONObject(new String(buffer));
            return  entitys;
            
            
        }
    
     //获取单个对象,封装基于HttpGet
     public JSONObject GetVehicle(String Uri) throws ClientProtocolException, IOException {
           
                // Send GET request to <service>/GetVehicle/<plate>
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpGet request = new HttpGet(Uri);

                request.setHeader("Accept", "application/json");
                request.setHeader("Content-type", "application/json");

                HttpResponse response = httpClient.execute(request);

                HttpEntity responseEntity = response.getEntity();

                // Read response data into buffer
                char[] buffer = new char[(int)responseEntity.getContentLength()];
                InputStream stream = responseEntity.getContent();
                InputStreamReader reader = new InputStreamReader(stream);
                reader.read(buffer);
                stream.close();

                JSONObject myentity=null;
                try {
                    myentity = new JSONObject(new String(buffer));
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return  myentity;
                
//               myentity.getString("make")
//              myentity.getString("plate");

                
            }
    
    
     //增加一个实体,封装基于HttpPost
     public  JSONObject   addEntity(String Uri, List ParDataList) throws IOException, JSONException
     {
        
         //创建一个http客户端
         HttpClient client=new DefaultHttpClient();  
         //创建一个PUT请求
         HttpPost httpPost=new HttpPost(Uri);
        
         httpPost.setHeader("Accept", "application/json");
         httpPost.setHeader("Content-type", "application/json");
         //组装数据放到HttpEntity中发送到服务器
//         final List dataList = new ArrayList();
//         dataList.add(new BasicNameValuePair("price", "11.99"));  
         HttpEntity entity = null;
        try {
            entity = new UrlEncodedFormEntity(ParDataList, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
        httpPost.setEntity(entity);
        
         HttpResponse response=null;
         try {
             response=client.execute(httpPost);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
         if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
           {
            System.out.println("OK");
           }
          else
          {
 
            String status=  response.getStatusLine().getStatusCode()+" ";
             System.out.println(status);
          }
        
        
        //返回的结果可能放到InputStream,http Header中等。
            InputStream inputStream=null;
        try {
             inputStream=response.getEntity().getContent();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
 
         
        char[] buffer = new char[(int)response.getEntity().getContentLength()];

        InputStreamReader reader = new InputStreamReader(inputStream);
      reader.read(buffer);
      inputStream.close();

      JSONObject entitys = new JSONObject(new String(buffer));
      return  entitys;
        
        
        
        
     }
        
     //修改实体,封装基于HttpPut
     public JSONObject  updateEntity(String Uri, List ParDataList) throws IOException, JSONException
     {
        
        //创建一个http客户端
        HttpClient client=new DefaultHttpClient();
         //创建一个PUT请求,如:http://www.store.com/product/1234
        HttpPut httpPut=new HttpPut(Uri);
        httpPut.setHeader("Accept", "application/json");
        httpPut.setHeader("Content-type", "application/json");
         HttpEntity entity = null;
            try {
                entity = new UrlEncodedFormEntity(ParDataList, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  
            httpPut.setEntity(entity);
            
             HttpResponse response=null;
             try {
                 response=client.execute(httpPut);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  
             if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
               {
                System.out.println("OK");
               }
              else
              {
    
                String status=  response.getStatusLine().getStatusCode()+" ";
                 System.out.println(status);
              }
            
            
            //返回的结果可能放到InputStream,http Header中等。
                InputStream inputStream=null;
            try {
                 inputStream=response.getEntity().getContent();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  
    
             
            char[] buffer = new char[(int)response.getEntity().getContentLength()];

            InputStreamReader reader = new InputStreamReader(inputStream);
          reader.read(buffer);
          inputStream.close();

          JSONObject entitys = new JSONObject(new String(buffer));
          return  entitys;    
            
        
     }
    
     //删除一个实体  ,基于HttpDelete ,"http://www.store.com/product/1234"
     public JSONObject  deleteEntity(String Uri) throws ClientProtocolException, IOException, JSONException
     {
        
         //创建一个http客户端
         HttpClient client=new DefaultHttpClient();
        
         //创建一个DELETE请求
         HttpDelete httpDelete=new HttpDelete(Uri);
         httpDelete.setHeader("Accept", "application/json");
         httpDelete.setHeader("Content-type", "application/json");
         //向服务器发送DELETE请求并获取服务器返回的结果,可能是删除成功,或者失败等信息
        HttpResponse response=client.execute(httpDelete);  
         if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
           {
          System.out.println("OK");
           }
          else
          {

            String status=  response.getStatusLine().getStatusCode()+" ";
             System.out.println(status);
          }
        
        
        //返回的结果可能放到InputStream,http Header中等。
            InputStream inputStream=null;
        try {
             inputStream=response.getEntity().getContent();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  

         
        char[] buffer = new char[(int)response.getEntity().getContentLength()];

        InputStreamReader reader = new InputStreamReader(inputStream);
    reader.read(buffer);
    inputStream.close();

    JSONObject entitys = new JSONObject(new String(buffer));
    return  entitys;
        
     }
    
    

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值