分享一个android 访问http的类,非常好用哦

/**
 * 这个类是一个Http操作类
 * @author liujing
 *
 */
public class MyHttpClient {
    /** 
     * 通过HttpClient发送GET请求 
     * @param path 请求路径 
     * @param params 请求参数 
     * @param ecoding 请求编码 
     * @return 请求是否成功 
     */  
    public HttpResponse sendHttpClientGETRequest(String path,Map<String, String> params, String ecoding) throws Exception {  
        StringBuilder url=new StringBuilder(path);  
        url.append("?");  
        for (Map.Entry<String, String> entry : params.entrySet()) {  
            url.append(entry.getKey()).append("=");  
            url.append(URLEncoder.encode(entry.getValue(),ecoding));  
            url.append("&");  
        }  
        url.deleteCharAt(url.length()-1);  
        HttpGet httpGet=new HttpGet(url.toString());  
        DefaultHttpClient client=new DefaultHttpClient();  
        client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
        client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000);
        HttpResponse response=client.execute(httpGet); 
        if(response.getStatusLine().getStatusCode()==200){  
            return response;  
        }  
        return null;  
    } 

        /** 
    * 通过HttpClient发送Post请求 
    * @param path 请求路径 
    * @param params 请求参数 
    * @param ecoding 请求编码 
    * @return 请求是否成功 
    */  
    public HttpResponse sendHttpClientPOSTRequest(String path,  
           Map<String, String> params, String ecoding) throws Exception {  
       List<NameValuePair> pair=new ArrayList<NameValuePair>();//存放请求参数  
       if(params!=null && !params.isEmpty()){  
           for (Map.Entry<String, String> entry : params.entrySet()) {  
               pair.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));  
           }  
       }  
       UrlEncodedFormEntity enFormEntity=new UrlEncodedFormEntity(pair,ecoding);  
       HttpPost httpPost=new HttpPost(path);  
       httpPost.setEntity(enFormEntity);  
       DefaultHttpClient client=new DefaultHttpClient();  
       client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
       client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000);
       HttpResponse response=client.execute(httpPost);  
       if(response.getStatusLine().getStatusCode()==200){  
           return response;  
       } 
       return null;  
    }

    /** 
    * 通过HttpClient发送Post请求 
    * @param path 请求路径 
    * @param params 请求参数 
    * @param ecoding 请求编码 
    * @return 请求是否成功 
    */  
    public HttpResponse sendHttpClientPOSTRequest(String path,  
           Map<String, String> params, String ecoding, int timeout) throws Exception {  
       List<NameValuePair> pair=new ArrayList<NameValuePair>();//存放请求参数  
       if(params!=null && !params.isEmpty()){  
           for (Map.Entry<String, String> entry : params.entrySet()) {  
               pair.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));  
           }  
       }  
       UrlEncodedFormEntity enFormEntity=new UrlEncodedFormEntity(pair,ecoding);  
       HttpPost httpPost=new HttpPost(path);  
       httpPost.setEntity(enFormEntity);  
       DefaultHttpClient client=new DefaultHttpClient();  
       client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
       client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
       HttpResponse response=client.execute(httpPost);  
       if(response.getStatusLine().getStatusCode()==200){  
           return response;  
       } 
       return null;  
    }
}

就不解释了,直接用就好了,返回值是HttpResponse,建议服务端直接返回json数据,完了端这边直接解析

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值