http请求工具类
http请求工具类:
public static JSONObject httpPost(String url,String json, boolean noNeedResponse){
DefaultHttpClient httpClient = new DefaultHttpClient();
JSONObject jsonResult = null;
HttpPost method = new HttpPost(url);
try {
if (null != json) {
StringEntity entity = new StringEntity(json, "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
method.setEntity(entity);
}
HttpResponse result = httpClient.execute(method);
url = URLDecoder.decode(url, "UTF-8");
if (result.getStatusLine().getStatusCode() == 200) {
String str = "";
try {
str = EntityUtils.toString(result.getEntity());
if (noNeedResponse) {
return null;
}
jsonResult = JSONObject.fromObject(str);
} catch (Exception e) {
logger.error(url, e);
}
}
} catch (IOException e) {
logger.error(url, e);
}
return jsonResult;
}