Java client sdk下载
示例工程下载
业务接口根据自己需要选择接口对接,下面举两个例:
- 学科查询
http://{api-gateway-host}/base/subjects
该接口和用户无关,无需带用户相关的open_id和access_token,Java代码示例:
public Object subjects() throws IOException {
HttpClient httpClient = NdHttpClientBuilder.getHttpClient(ACCESS_KEY_ID, SECRET_ACCESS_ID);//此类由sdk提供
HttpGet request = new HttpGet(API_GATEWAY_HOST + "/base/subjects");
request.setHeader("Content-Type", "application/json");
request.setHeader("sdp-app-id", SDP_APP_ID);
HttpHost httpHost = HttpHost.create(API_GATEWAY_HOST);
HttpResponse httpResponse = httpClient.execute(httpHost, request);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
String result = EntityUtils.toString(httpResponse.getEntity());
return JSONObject.parseArray(result);
}
return EntityUtils.toString(httpResponse.getEntity());
}
查询用户有权限的学校列表
http://{api-gateway-host}/base/schools/actions/search
该接口和用户相关,需要额外传入idp-open-id和idp-access-token,放到header里,也就是open_id和access_token。Java代码示例:
public Object schoolsPrivate(
Integer offset,
Integer limit,
String name,
String openid,
String accessToken) throws IOException {
HttpClient httpClient = NdHttpClientBuilder.getHttpClient(ACCESS_KEY_ID, SECRET_ACCESS_ID);//此类由sdk提供
HttpPost request = new HttpPost(String.format(API_GATEWAY_HOST + "/base/schools/actions/search?$offset=%s&$limit=%s", offset, limit));
request.setHeader("Content-Type", "application/json");
request.setHeader("sdp-app-id", SDP_APP_ID);
request.setHeader("idp-open-id", openid);
request.setHeader("idp-access-token", accessToken);
JSONObject obj = new JSONObject();
obj.put("name", name);
StringEntity entity = new StringEntity(obj.toString());
request.setEntity(entity);
HttpHost httpHost = HttpHost.create(API_GATEWAY_HOST);
HttpResponse httpResponse = httpClient.execute(httpHost, request);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
String result = EntityUtils.toString(httpResponse.getEntity());
return JSONObject.parseArray(result);
}
return EntityUtils.toString(httpResponse.getEntity());
}
作者:wangtc 创建时间:2023-10-08 10:41
更新时间:2023-11-14 11:11
更新时间:2023-11-14 11:11