本文介紹了如何使用Java SDK擷取個體列表資訊。
功能描述
每個個體必須屬於一個分組,在調用該介面時指定某個分組,則返回該分組下的所有個體ID列表。關於參數的詳細說明,請參見查詢個體列表API文檔。
您需要使用Alibaba Content Security Service的API接入地址,調用本SDK介面。關於API接入地址的資訊,請參見接入地址(Endpoint)。
前提條件
安裝Java依賴。關於安裝Java依賴的具體操作,請參見安裝Java依賴。
說明請一定按照安裝Java依賴頁面中的版本安裝,否則會導致調用失敗。
如果使用本地檔案或者二進位檔案檢測,請下載並在專案工程中引入Extension.Uploader工具類。
擷取個體列表程式碼範例
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.green.model.v20180509.GetPersonsRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
/**
* 擷取指定組下的個體Id列表。
*
*/
public class FaceGetPersonsRequestSample {
public static void main(String[] args) throws Exception {
/**
* 阿里雲帳號AccessKey擁有所有API的存取權限,建議您使用RAM使用者進行API訪問或日常營運。
* 常見擷取環境變數方式:
* 方式一:
* 擷取RAM使用者AccessKey ID:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
* 擷取RAM使用者AccessKey Secret:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
* 方式二:
* 擷取RAM使用者AccessKey ID:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
* 擷取RAM使用者AccessKey Secret:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
*/
DefaultProfile profile = DefaultProfile.getProfile(
"cn-shanghai",
"建議從環境變數中擷取RAM使用者AccessKey ID",
"建議從環境變數中擷取RAM使用者AccessKey Secret");
DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
// 注意:此處執行個體化的client儘可能重複使用,提升檢測效能。避免重複建立串連。
IAcsClient client = new DefaultAcsClient(profile);
GetPersonsRequest request = new GetPersonsRequest();
request.setAcceptFormat(FormatType.JSON); // 指定API返回格式。
request.setMethod(com.aliyuncs.http.MethodType.POST); // 指定要求方法。
request.setEncoding("utf-8");
JSONObject data = new JSONObject();
/**
* groupId: 使用者自訂群組ID,必填。
*/
data.put("groupId", "java_sdk_test_group");
request.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
/**
* 請務必設定逾時時間。
*/
request.setConnectTimeout(3000);
request.setReadTimeout(6000);
try {
HttpResponse httpResponse = client.doAction(request);
if (httpResponse.isSuccess()) {
JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
System.out.println(JSON.toJSONString(scrResponse, true));
if (200 == scrResponse.getInteger("code")) {
JSONObject resultObject = scrResponse.getJSONObject("data");
if (200 == resultObject.getInteger("code")) {
System.out.println(resultObject.getString("personIds"));
} else {
System.out.println("task process fail:" + resultObject.getInteger("code"));
}
} else {
System.out.println("detect not success. code:" + scrResponse.getInteger("code"));
}
} else {
System.out.println("response not success. status:" + httpResponse.getStatus());
}
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}