Android网络编程工具类:HttpUtil

import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class HttpUtil {

	/**
	 * Login链接服务器的URL
	 */
	public final static String BASE_URL="http://192.168.2.100:8088/LocomotePoliceWebService";
	
	/**
	 * 通过URL获得HttpGet对象
	 * @param url
	 * @return
	 */
	public static HttpGet getHttpGet(String url){
		LogUtil.debug("HttpUtil-->getHttpGet method starting");
		LogUtil.debug("HttpUtil-->input url string is "+url);
		//实例化
		HttpGet request = new HttpGet(url);
		LogUtil.debug("HttpUtil-->HttpGet instance:"+request);
		return request;
	}
	
	/**
	 * 通过url获得HttpPost对象
	 * @param url
	 * @return
	 */
	public static HttpPost getHttpPost(String url){
		LogUtil.debug("HttpUtil-->getHttpPost method starting");
		LogUtil.debug("HttpUtil-->input url string is "+url);
		HttpPost request = new HttpPost(url);
		return request;
	}
	
	/**
	 * 通过HttpGet获HttpResponse对象
	 * @param request
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static HttpResponse getHttpResponse(HttpGet request) throws ClientProtocolException, IOException{
		LogUtil.debug("HttpUtil-->getHttpResponse method is starting!");
		HttpResponse response = new DefaultHttpClient().execute(request);
		LogUtil.debug("HttpUtil-->HttpResponse instance:"+response);
		return response;
	}
	
	/**
	 * 通过HttpPost获得HttpResponse对象
	 * @param request
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static HttpResponse getHttpResponse(HttpPost request) throws ClientProtocolException,IOException{
		LogUtil.debug("HttpUtil-->getHttpResponse method starting");
		HttpResponse response = new DefaultHttpClient().execute(request);
		LogUtil.debug("HttpUtil-->getHttpResponse instance:"+response);
		return response;
	}
	
	/**
	 * 通过URL发送post请求,返回请求结果
	 */
	public static String queryStringForPost(String url){
		LogUtil.debug("HttpUtil-->queryStringForPost method starting");
		LogUtil.debug("HttpUtil-->input url string is "+url);
		//获得HttpPost实例
		HttpPost httpPost = HttpUtil.getHttpPost(url);
		StringBuilder resultStr = new StringBuilder();
		try {
			HttpResponse response = HttpUtil.getHttpResponse(httpPost);
			//判断请求是否成功
			if(response.getStatusLine().getStatusCode()==200){
				LogUtil.debug("HttpUtil-->查询成功");
				//获得返回结果
				resultStr.append(EntityUtils.toString(response.getEntity()));
				LogUtil.debug("HttpUtil-->返回的结果:"+resultStr.toString());
				return resultStr.toString();
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
			LogUtil.debug("HttpUtil-->网络异常");
			LogUtil.debug("HttpUtil-->查询失败");
			LogUtil.debug(e.toString());
			return resultStr.append("网络异常").toString();
		} catch (IOException e) {
			e.printStackTrace();
			LogUtil.debug("HttpUtil-->网络异常");
			LogUtil.debug("HttpUtil-->查询失败");
			LogUtil.debug(e.toString());
			return resultStr.append("网络异常").toString();
		}
		LogUtil.debug("HttpUtil-->最终返回的结果:"+resultStr.toString());
		return resultStr.toString();
	}
	
	/**
	 * 通过HttpPost发送Get请求,返回请求结果
	 */
	public static String queryStringForGet(String url){
		LogUtil.debug("HttpUtil-->queryStringForGet method starting");
		LogUtil.debug("HttpUtil-->input url string is "+url);
		//获得HttpGet对象
		HttpGet httpGet = HttpUtil.getHttpGet(url);
		StringBuilder resultSb = new StringBuilder();
		try {
			HttpResponse response = HttpUtil.getHttpResponse(httpGet);
			if(response.getStatusLine().getStatusCode()==200){
				LogUtil.debug("HttpUtil-->查询成功");
				//获得请求结果
				resultSb = new StringBuilder(EntityUtils.toString(response.getEntity()));
				LogUtil.debug("HttpUtil-->返回的结果:"+resultSb.toString());
				return resultSb.toString();
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
			LogUtil.debug("HttpUtil-->网络异常");
			LogUtil.debug("HttpUtil-->查询失败");
			LogUtil.debug(e.toString());
			return resultSb.append("网络异常").toString();
		} catch (IOException e) {
			e.printStackTrace();
			LogUtil.debug("HttpUtil-->网络异常");
			LogUtil.debug("HttpUtil-->查询失败");
			LogUtil.debug(e.toString());
			return resultSb.append("网络异常").toString();
		}
		LogUtil.debug("HttpUtil-->最终返回的结果:"+resultSb.toString());
		return resultSb.toString();
	}
	
	public static String queryStringForPost(HttpPost request){
		String result= null;
		try {
			HttpResponse response = HttpUtil.getHttpResponse(request);
			//判断是否请求成功
			if(response.getStatusLine().getStatusCode()==200){
				//获得返回结果
				result = EntityUtils.toString(response.getEntity());
				return result;
			}
		} catch (ClientProtocolException e) {
			result="网络异常";
			e.printStackTrace();
			return result;
		} catch (IOException e) {
			result="网络异常";
			e.printStackTrace();
			return result;
		}
		return result;
	}
	
	
}

  and

转载于:https://www.cnblogs.com/mayzhang/archive/2013/05/02/3055715.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值