package 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 { public static final String BASE_URL="http://192.168.10.57:8080/LBSServer/"; public static HttpGet getHttpGet(String url) { HttpGet request = new HttpGet(url); return request; } public static HttpPost getHttpPost(String url) { HttpPost request = new HttpPost(url); return request; } public static HttpResponse getHttpResponse(HttpGet request) throws ClientProtocolException, IOException { HttpResponse response = new DefaultHttpClient().execute(request); return response; } public static HttpResponse getHttpResponse(HttpPost request) throws ClientProtocolException, IOException { HttpResponse response = new DefaultHttpClient().execute(request); return response; } public static String queryStringForPost(String url) { HttpPost request = HttpUtil.getHttpPost(url); String result = null; try { HttpResponse response = HttpUtil.getHttpResponse(request); if( 200 == response.getStatusLine().getStatusCode()) { result = EntityUtils.toString(response.getEntity()); return result; } } catch (ClientProtocolException e) { e.printStackTrace(); result = "Failed in queryStringForPost!"; return result; } catch (IOException e) { e.printStackTrace(); result = "Failed in queryStringForPost!"; return result; } return null; } public static String queryStringForPost(HttpPost request) { String result = null; try { HttpResponse response = HttpUtil.getHttpResponse(request); if( 200 == response.getStatusLine().getStatusCode()) { result = EntityUtils.toString(response.getEntity()); return result; } } catch (ClientProtocolException e) { e.printStackTrace(); result = "Failed in queryStringForPost!"; return result; } catch (IOException e) { e.printStackTrace(); result = "Failed in queryStringForPost!"; return result; } return null; } public static String queryStringForGet(String url) { HttpGet request = HttpUtil.getHttpGet(url); String result = null; try { HttpResponse response = HttpUtil.getHttpResponse(request); if( 200 == response.getStatusLine().getStatusCode()) { result = EntityUtils.toString(response.getEntity()); return result; } } catch (ClientProtocolException e) { e.printStackTrace(); result = "Failed in queryStringForGet!"; return result; } catch (IOException e) { e.printStackTrace(); result = "Failed in queryStringForGet!"; return result; } return null; }}