// public class HttpUtil {
public static String doPost(String url, JSONObject param) {
HttpPost httpPost = null;
String result = null;
try {
HttpClient client = new DefaultHttpClient();
httpPost = new HttpPost(url);
if (param != null) {
StringEntity se = new StringEntity(param.toString(), "utf-8");
httpPost.setEntity(se); // post方法中,加入json数据
httpPost.setHeader("Content-Type", "application/json");
//httpPost.setHeader("X-Access-Token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MDM5MzY1NzcsInVzZXJuYW1lIjoiYWRtaW4ifQ.YLw1DXgdiFVBt2UqRfAvM6StGZ6o9ggRC3Xs4-SYPZw");
}
HttpResponse response = client.execute(httpPost);
if (response != null) {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity, "utf-8");
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
public static String doGet(String url) {
HttpGet httpGet = null;
String result = null;
try {
HttpClient client = new DefaultHttpClient();
httpGet = new HttpGet(url);
httpGet.setHeader("Content-Type", "application/json");
//todo 是否要token
httpGet.setHeader("X-Access-Token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MDM5MzY1NzcsInVzZXJuYW1lIjoiYWRtaW4ifQ.YLw1DXgdiFVBt2UqRfAvM6StGZ6o9ggRC3Xs4-SYPZw");
HttpResponse response = client.execute(httpGet);
if (response != null) {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity, "utf-8");
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
java post get调用接口
最新推荐文章于 2024-07-17 16:07:45 发布