httpclient是什么这里不再详述(可参考最下方网址);直接上代码;接口返回json格式的数据。
参考自:http://blog.csdn.net/wangpeng047/article/details/19624529
package com.httpclient.test;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class ThttpClient {
public static void main(String[] args) {
String baseURL = "http://192.168.17.75:8080";
String token = "66b5c33d-4d6b-49cd-bb54-303b6d0780b5";
CloseableHttpClient httpclient = HttpClients.createDefault();
createFaciGraphSession(httpclient, baseURL, token);
httpclient = HttpClients.createDefault();
String result = (String) getBlendingPlanList(httpclient, baseURL, token, 1L);
System.out.println(result);
}
private static void createFaciGraphSession(CloseableHttpClient httpclient, String baseURL, String token) {
// 创建默认的httpClient实例.
// CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet(baseURL + "/system/sessionManage/createSession?_token="+token);
// 执行get请求.
try {
CloseableHttpResponse res = httpclient.execute(httpget);
// CloseableHttpResponse res = httpclient.execute(httppost);
try {
HttpEntity entity = res.getEntity();
if (entity != null) {
System.out.println("----------------------------");
System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8"));
System.out.println("--------------------------------");
}
} finally {
res.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static Object getBlendingPlanList(CloseableHttpClient httpclient, String baseURL, String token, Long planId){
String result = null;
// HttpGet httpget = new HttpGet(baseURL + "/third/planInfo/getBlendingPlanIds?_token="+token+"&planId=" + planId);
HttpGet httpget = new HttpGet(baseURL + "/third/planInfo/getBlendingPlanList?_token="+token+"&planId=" + planId);
// 执行get请求.
try {
CloseableHttpResponse res = httpclient.execute(httpget);
// CloseableHttpResponse res = httpclient.execute(httppost);
try {
HttpEntity entity = res.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity, "UTF-8");
}
} finally {
res.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
}
参考自:http://blog.csdn.net/wangpeng047/article/details/19624529