android使用apache httpclient发送post请求

<span style="font-family:Arial, Helvetica, sans-serif;"><span style="white-space: normal;">
</span></span>package com.liuc;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;


public class HttpUtil {
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * get请求
<span style="white-space:pre">	</span> * @param map
<span style="white-space:pre">	</span> * @param encode
<span style="white-space:pre">	</span> * @param path
<span style="white-space:pre">	</span> * @return
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>public static String sendHttpGet(String encode,String path){
<span style="white-space:pre">		</span>try {
<span style="white-space:pre">			</span>HttpGet httget = new HttpGet();  
<span style="white-space:pre">			</span>httget.setURI(new URI(path));
            
<span style="white-space:pre">			</span>RequestConfig requestConfig = RequestConfig.custom().
<span style="white-space:pre">					</span>setSocketTimeout(2000).setConnectTimeout(2000).build();//4.3之后的设置请求和传输超时时间
<span style="white-space:pre">			</span>httget.setConfig(requestConfig);
<span style="white-space:pre">			</span>HttpClient httpClient = HttpClients.createDefault();
<span style="white-space:pre">			</span>HttpResponse httpResponse=httpClient.execute(httget);
<span style="white-space:pre">			</span>if (httpResponse.getStatusLine().getStatusCode()==200) {
<span style="white-space:pre">				</span>return getStringForInputStream(httpResponse.getEntity().getContent(),encode);
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>
<span style="white-space:pre">		</span>} catch (UnsupportedEncodingException e) {
<span style="white-space:pre">			</span>e.printStackTrace();
<span style="white-space:pre">		</span>} catch (ClientProtocolException e) {
<span style="white-space:pre">			</span>e.printStackTrace();
<span style="white-space:pre">		</span>} catch (IOException e) {
<span style="white-space:pre">			</span>e.printStackTrace();
<span style="white-space:pre">		</span>} catch (URISyntaxException e) {
<span style="white-space:pre">			</span>e.printStackTrace();
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>return path;
<span style="white-space:pre">		</span>
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * post请求
<span style="white-space:pre">	</span> * @param map
<span style="white-space:pre">	</span> * @param encode
<span style="white-space:pre">	</span> * @param path
<span style="white-space:pre">	</span> * @return
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>public static String sendHttpPost(Map<String, <span style="white-space:pre">	</span>String> map,String encode,String path){
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>List<NameValuePair> list=new ArrayList<NameValuePair>();
<span style="white-space:pre">		</span>if (map!=null&&!map.isEmpty()) {
<span style="white-space:pre">			</span>for(Map.Entry<String, String> entry:map.entrySet()){
<span style="white-space:pre">				</span>list.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
<span style="white-space:pre">			</span>}
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>try {
<span style="white-space:pre">			</span>//实现将参数封装到表单中,也就是请求体重
<span style="white-space:pre">			</span>UrlEncodedFormEntity entity=new UrlEncodedFormEntity(list,encode);
<span style="white-space:pre">			</span>//使用post方式提交数据
<span style="white-space:pre">			</span>HttpPost httpPost=new HttpPost(path);
<span style="white-space:pre">			</span>httpPost.setEntity(entity);
<span style="white-space:pre">			</span>RequestConfig requestConfig = RequestConfig.custom().
<span style="white-space:pre">					</span>setSocketTimeout(2000).setConnectTimeout(2000).build();//4.3之后的设置请求和传输超时时间
<span style="white-space:pre">			</span>httpPost.setConfig(requestConfig);
<span style="white-space:pre">			</span>//4.x是这样的
<span style="white-space:pre">			</span>HttpClient httpClient = HttpClients.createDefault();
<span style="white-space:pre">			</span>HttpResponse httpResponse=httpClient.execute(httpPost);
<span style="white-space:pre">			</span>
<span style="white-space:pre">			</span>if (httpResponse.getStatusLine().getStatusCode()==200) {
<span style="white-space:pre">				</span>return getStringForInputStream(httpResponse.getEntity().getContent(),encode);
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>
<span style="white-space:pre">		</span>} catch (UnsupportedEncodingException e) {
<span style="white-space:pre">			</span>e.printStackTrace();
<span style="white-space:pre">		</span>} catch (ClientProtocolException e) {
<span style="white-space:pre">			</span>e.printStackTrace();
<span style="white-space:pre">		</span>} catch (IOException e) {
<span style="white-space:pre">			</span>e.printStackTrace();
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>return path;
<span style="white-space:pre">		</span>
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>private static String getStringForInputStream(InputStream inputStream,
<span style="white-space:pre">			</span>String encode) {
<span style="white-space:pre">		</span>ByteArrayOutputStream stream=new ByteArrayOutputStream();
<span style="white-space:pre">		</span>byte[] data=new byte[1024];
<span style="white-space:pre">		</span>int len=0;
<span style="white-space:pre">		</span>String result="";
<span style="white-space:pre">		</span>try {
<span style="white-space:pre">			</span>if (inputStream != null) {
<span style="white-space:pre">				</span>while ((len = inputStream.read(data)) != -1) {
<span style="white-space:pre">					</span>stream.write(data, 0, len);
<span style="white-space:pre">				</span>}
<span style="white-space:pre">				</span>result=new String(stream.toByteArray(),encode);
<span style="white-space:pre">			</span>}
<span style="white-space:pre">		</span>} catch (Exception e) {
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>return result;
<span style="white-space:pre">	</span>}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值