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