Android 工具类httpClient

jar包下载

httppost

private static void Httppost() {
//创建HttpClientBuilder  
HttpClientBuilder newBuilder = HttpClientBuilder.create();  
CloseableHttpClient newClient = newBuilder.build();  
//接口地址
String url="";
//httppost
HttpPost newHttppost=new HttpPost(url);
//默认配置post
newHttppost.setConfig(RequestConfig.DEFAULT);
//post数据
String postString="";
//post实体
StringEntity entity=new StringEntity(postString,"UTF-8");
try {
   newHttppost.setEntity(entity);
   //执行httppost请求
   HttpResponse httpResponse=newClient.execute(newHttppost);
 //获取响应实体
    HttpEntity httpEntity=httpResponse.getEntity();
    //响应状态
    System.out.println("StatusLine: " +httpResponse.getStatusLine());
     if (httpEntity != null) {  
            System.out.println("contentEncoding:" + 
            httpEntity.getContentEncoding()); 
            System.out.println("response content:" + 
            EntityUtils.toString(httpEntity));
        }  
    } catch (IOException e) {  
        e.printStackTrace();  
    } finally {  
        try {  
        //关闭流并释放资源  
            newClient.close();  
    } catch (IOException e) {  
        e.printStackTrace();  
    }  
}  
}

httpget

private static void HttpGet() {
//创建Client
    HttpClientBuilder newBuilder=HttpClientBuilder.create();
    CloseableHttpClient newClient=newBuilder.build();
    //Httpget
    HttpGet newHttpget=new HttpGet("http://hq.sinajs.cn/list=sh601006");
    System.out.println("RequestLint: " + newHttpget.getRequestLine());
    try {
        //执行get请求
        HttpResponse httpResponse=newClient.execute(newHttpget);
        //获取响应实体
        HttpEntity httpEntity=httpResponse.getEntity();
        //响应状态
        System.out.println("StatusLine: " +httpResponse.getStatusLine());
         if (httpEntity != null) {  
                System.out.println("contentEncoding:" + 
                httpEntity.getContentEncoding());
                //将返回实体字符串输出
                System.out.println("response content:" + 
                EntityUtils.toString(httpEntity));
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            try {  
            //关闭流并释放资源  
                newClient.close();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
}
/**
RequestLint: GET http://hq.sinajs.cn/list=sh601006 HTTP/1.1
StatusLine: HTTP/1.1 200 OK
contentEncoding:null
response content:var hq_str_sh601006="..."
*/

传输实体

  1. UrlEncodedFormEntity()
List<NameValuePair> pairs = new ArrayList<NameValuePair>();  
NameValuePair pair1 = new BasicNameValuePair("supervisor", 
supervisorEt.getEditableText().toString());      
pairs.add(pair1);        
httpPost.setEntity(new UrlEncodedFormEntity(pairs, HTTP.UTF_8));
  1. StringEntity()
    httpPost.setEntity(new StringEntity(postData.toString(), HTTP.UTF_8));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值