Android使用httpClient进行Post和Get发送参数

模拟发送Http请求我们可以使用HttpURLConnection类进行操作,但是Android平台集成了功能强大且编写更容易的commons-httpclient.jar,因此在这里介绍如何通过commons-httpclient进行Http请求。发送Http请求可以有两种方式:一种是同步,一种是异步。由于我对异步不是很熟悉,所以这里先提供同步方式发送Http请求:

1、使用Get方式发送

public String httpGet(String url, String params) throws Exception 
    { 
        String response = null; //返回信息 
        if (null!=params&&!params.equals("")) 
        { 
            url += "?" + params; 
        } 
        // 构造HttpClient的实例 
        HttpClient httpClient = new HttpClient(); 
        // 创建GET方法的实例 
        GetMethod httpGet = new GetMethod(url); 
        // 设置超时时间 
        httpGet.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, new Integer(CONNECTION_TIMEOUT)); 
        try
        { 
            int statusCode = httpClient.executeMethod(httpGet); 
            if (statusCode == HttpStatus.SC_OK) //SC_OK = 200 
            { 
                InputStream inputStream = httpGet.getResponseBodyAsStream(); //获取输出流,流中包含服务器返回信息 
                response = getData(inputStream);//获取返回信息 
            } 
            else
            { 
                LOG.debug("Get Method Statuscode : "+statusCode); 
            } 
        } catch (Exception e) 
        { 
            throw new Exception(e); 
        } finally
        { 
            httpGet.releaseConnection(); 
            httpClient = null; 
        } 
        return response; 
    } 


2、使用Post方式发送

public String httpPost(String url, List<Parameter> params) throws Exception 
    { 
        String response = null; 
        HttpClient httpClient = new HttpClient(); 
        PostMethod httpPost = new PostMethod(url); 
        //Post方式我们需要配置参数 
        httpPost.addParameter("Connection", "Keep-Alive"); 
        httpPost.addParameter("Charset", "UTF-8"); 
        httpPost.addParameter("Content-Type", "application/x-www-form-urlencoded"); 
        httpPost.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, new Integer(CONNECTION_TIMEOUT)); 
        if (null!=params&¶ms.size()!=0) 
        { 
            //设置需要传递的参数,NameValuePair[] 
            httpPost.setRequestBody(buildNameValuePair(params)); 
        } 
        try
        { 
            int statusCode = httpClient.executeMethod(httpPost); 
            if (statusCode == HttpStatus.SC_OK) 
            { 
                InputStream inputStream = httpPost.getResponseBodyAsStream(); 
                response = getData(inputStream); 
            } 
            else
            { 
                LOG.debug("Post Method Statuscode : "+statusCode); 
            } 
        } catch (Exception e) 
        { 
            throw new Exception(e); 
        } finally
        { 
            httpPost.releaseConnection(); 
            httpClient = null; 
        } 
        return response; 
    } 


3、构件NameValuePair

private NameValuePair[] buildNameValuePair(List<Parameter> params) 
    { 
        int size = params.size(); 
        NameValuePair[] pair = new NameValuePair[size]; 
        for(int i = 0 ;i<size;i++) 
        { 
            Parameter param = params.get(i); 
            pair[i] = new NameValuePair(param.getName(),param.getValue()); 
        } 
        return pair; 
    } 


4、获得返回数据

private String getData(InputStream inputStream) throws Exception 
    { 
        String data = ""; 
        //内存缓冲区 
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
        int len = -1; 
        byte[] buff = new byte[1024]; 
        try
        { 
            while((len=inputStream.read(buff))!=-1) 
            { 
                outputStream.write(buff, 0, len); 
            } 
            byte[] bytes = outputStream.toByteArray(); 
            data = new String(bytes); 
        } catch (IOException e) 
        { 
            throw new Exception(e.getMessage(),e); 
        } 
        finally
        { 
            outputStream.close(); 
        } 
        return data; 
    } 


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值