import java.io.IOException;
import java.util.Date;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class PostClient
{
public static JSONObject postCallWebService(String url, String postVal){
JSONObject tmpObj = new JSONObject();
Date begin = new Date();
HttpClient client = PoolManager.getInstance().getHttpClient();
String resultValues = "";
HttpPost post = new HttpPost(url);
post.setHeader("Content-type", "application/json;charset=utf-8");
StringEntity entity = new StringEntity(postVal,java.nio.charset.Charset.forName("UTF-8"));
post.setEntity(entity);
try {
HttpResponse response = client.execute(post);
HttpEntity responseEntity = response.getEntity();
resultValues = EntityUtils.toString(responseEntity);
}
catch (Exception e)
{
try {
tmpObj.put("httpStatusCode", "500");
tmpObj.put("data", new JSONArray());
tmpObj.put("error", new JSONArray());
tmpObj.put("errorMessage", e.getMessage());
} catch (JSONException e1) {
Log.error(e1);
}
} finally {
post.releaseConnection();
}
Log.info(">>>>>>调用接口" + url + "结束,耗时(毫秒):" + (
new Date().getTime() - begin.getTime()) + " 返回结果:" + resultValues);
return tmpObj;
}
}
Java json格式数据的post请求
最新推荐文章于 2024-07-26 03:42:47 发布