Java client sdk下载

示例工程下载

业务接口根据自己需要选择接口对接,下面举两个例:

  • 学科查询
  1. http://{api-gateway-host}/base/subjects

该接口和用户无关,无需带用户相关的open_id和access_token,Java代码示例:

  1. public Object subjects() throws IOException {
  2. HttpClient httpClient = NdHttpClientBuilder.getHttpClient(ACCESS_KEY_ID, SECRET_ACCESS_ID);//此类由sdk提供
  3. HttpGet request = new HttpGet(API_GATEWAY_HOST + "/base/subjects");
  4. request.setHeader("Content-Type", "application/json");
  5. request.setHeader("sdp-app-id", SDP_APP_ID);
  6. HttpHost httpHost = HttpHost.create(API_GATEWAY_HOST);
  7. HttpResponse httpResponse = httpClient.execute(httpHost, request);
  8. if (httpResponse.getStatusLine().getStatusCode() == 200) {
  9. String result = EntityUtils.toString(httpResponse.getEntity());
  10. return JSONObject.parseArray(result);
  11. }
  12. return EntityUtils.toString(httpResponse.getEntity());
  13. }
  • 查询用户有权限的学校列表

    1. http://{api-gateway-host}/base/schools/actions/search

    该接口和用户相关,需要额外传入idp-open-id和idp-access-token,放到header里,也就是open_id和access_token。Java代码示例:

  1. public Object schoolsPrivate(
  2. Integer offset,
  3. Integer limit,
  4. String name,
  5. String openid,
  6. String accessToken) throws IOException {
  7. HttpClient httpClient = NdHttpClientBuilder.getHttpClient(ACCESS_KEY_ID, SECRET_ACCESS_ID);//此类由sdk提供
  8. HttpPost request = new HttpPost(String.format(API_GATEWAY_HOST + "/base/schools/actions/search?$offset=%s&$limit=%s", offset, limit));
  9. request.setHeader("Content-Type", "application/json");
  10. request.setHeader("sdp-app-id", SDP_APP_ID);
  11. request.setHeader("idp-open-id", openid);
  12. request.setHeader("idp-access-token", accessToken);
  13. JSONObject obj = new JSONObject();
  14. obj.put("name", name);
  15. StringEntity entity = new StringEntity(obj.toString());
  16. request.setEntity(entity);
  17. HttpHost httpHost = HttpHost.create(API_GATEWAY_HOST);
  18. HttpResponse httpResponse = httpClient.execute(httpHost, request);
  19. if (httpResponse.getStatusLine().getStatusCode() == 200) {
  20. String result = EntityUtils.toString(httpResponse.getEntity());
  21. return JSONObject.parseArray(result);
  22. }
  23. return EntityUtils.toString(httpResponse.getEntity());
  24. }
作者:wangtc  创建时间:2020-06-18 18:25
 更新时间:2023-11-14 10:57