Appearance
Java调用示例
通过maven引入sdk
java
//引入接口平台依赖包
<dependency>
<groupId>cn.com.digitalhainan</groupId>
<artifactId>apione-http-client</artifactId>
<version>hzt-1.0.11-RELEASE</version>
</dependency>
或者通过导入apione-http-client-hzt-1.0.11-RELEASE.jar
POST请求示例
java
String ak = ""; //开放平台发放的accessKey
String sk = ""; //开放平台发放的secretKey
ContentBody contentBody = new ContentBody("accountIds=1558137&accountIds=1558410&tenantId=1");
//拼装业务信息
HttpParameters parameters = HttpParameters.builder()
.api("/mozi/employee/listGovEmployeeCodesByAccountIds")
.region("INTER")
.accessKey(ak)
.secretKey(sk)
.contentBody(contentBody)
.requestUrl("https://api-one.digitalhainan.com.cn/apione")
.mediaType(MediaType.parse("application/x-www-form-urlencoded"))
.build();
HttpReturn call = HttpCaller.getInstance().call(parameters);
System.out.println(
call.response
);
GET请求示例
java
String ak = ""; //开放平台发放的accessKey
String sk = ""; //开放平台发放的secretKey
Map<String, String> queryMap = new HashMap<>();
queryMap.put("tenantId", "1");
HttpParameters parameters = HttpParameters.builder()
.api("/auth/scopesV2")
.region("INTER")
.accessKey(ak)
.secretKey(sk)
.method("GET")
.queryParamsMap(queryMap)
.requestUrl("https://api-one.digitalhainan.com.cn/apione")
.build();
HttpReturn call = HttpCaller.getInstance().call(parameters);
System.out.println(
call.response
);
文件上传示例
java
String fileName = "test.txt";
File file = new File("/Users/caide/Desktop/" + fileName);
//header
//access_token 使用gettoken.json获取
Map<String, String> queryParam = new HashMap<>();
queryParam.put("access_token","app_90fc2b61fd344acab0555292c5902d8f");
queryParam.put("type","file");
queryParam.put("media","test");
//待上传文件
Map<String, AttachFile> attachFileMap = new HashMap<>();
AttachFile attachFile = new AttachFile();
attachFile.setFileName(fileName);
attachFile.setMediaType(MediaType.parse("application/octet-stream"));
attachFile.setFileBytes(FileUtil.readBytes(file));
// 固定写死media
attachFileMap.put("media", attachFile);
//拼装业务信息
HttpParameters parameters = HttpParameters.builder()
.api("/media/upload")
.region("INTER")
.attachFileMaps(attachFileMap)
.queryParamsMap(queryParam)
.formParamsMap(queryParam)
.mediaType(MediaType.parse("multipart/form-data"))//默认application/json
.accessKey("xxx")
.secretKey("xxx")
.requestUrl("https://api-one.digitalhainan.com.cn/apione")
.build();
//请求开发服务,获取response
HttpReturn call = HttpCaller.getInstance().call(parameters);
System.out.println(call.getResponse());
文件下载请求示例
java
Map<String, String> queryParam = new HashMap<>();
queryParam.put("access_token","app_90fc2b61fd344acab0555292c5902d8f");
queryParam.put("media_id","$iwHRCqcCrG9jdGV0LXN0cmVhbQMGBAAFAAa8kVGJ6vH3BhzNJBDEVh6WAl8xOGRmMmI5N2JiZQcACAAJoAoACwc");
HttpParameters parameters = HttpParameters.builder()
.api("/media/download")
.region("INTER")
.method("GET")
.queryParamsMap(queryParam)
.accessKey("xxx")
.secretKey("xxx")
.requestUrl("https://api-one.digitalhainan.com.cn/apione")
.build();
HttpByteReturn call = HttpCaller.getInstance().callByte(parameters);
System.out.println(
new String(call.getResponse().body().bytes(), "utf-8")
);