介绍如何使用Alibaba Cloud SDK for Java获取个体信息。

前提条件

在使用本教程之前,请确保已完成以下操作:
  • 使用Alibaba Cloud SDK for Java,您需要一个阿里云账号和访问密钥(AccessKey)。 请在阿里云控制台中的AccessKey管理页面上创建和查看您的AccessKey。
  • 确保您已经安装了Alibaba Cloud SDK for Java,准确的SDK版本号,请参见 阿里云开发工具包(SDK)
    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.4.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.61</version>
        </dependency>
    </dependencies>

代码示例

获取个体信息的示例代码如下所示:
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

public class FaceGetPersonRequestSample {
    public static void main(String[] args) {
        IClientProfile profile = DefaultProfile.getProfile(
                "<your-region-id>", // 您的可用区ID
                "<your-access-key-id>", // 您的AccessKey ID
                "<your-access-key-secret>"); // 您的AccessKey Secret
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        // 指定请求方式
        request.setSysMethod(MethodType.POST);
        // 指定请求地址
        request.setSysDomain("green.cn-hangzhou.aliyuncs.com");
        // 指定请求方法
        request.setSysUriPattern("/green/sface/person");
        // 指定请求版本
        request.setSysVersion("2018-05-09");
        // 支持HTTP和HTTPS
        request.setSysProtocol(ProtocolType.HTTPS);

        // 组装请求参数
        JSONObject httpBody = new JSONObject();
        // 目标个体ID
        httpBody.put("personId", "person1");
        request.setHttpContent(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(httpBody.toJSONString()),
                "UTF-8", FormatType.JSON);

        // 请设置超时时间, 服务端全链路处理超时时间为10秒,请做相应设置
        // 如果您设置的ReadTimeout小于服务端处理的时间,程序中会获得一个read timeout异常
        request.setSysConnectTimeout(3000);
        request.setSysReadTimeout(10000);
        try {
            CommonResponse response = client.getCommonResponse(request);
            JSONObject dataJson = JSON.parseObject(response.getData());
            System.out.println(JSON.toJSONString(dataJson, true));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }

    }
}

执行结果

正确的执行结果类似如下:
{
    "code":200,
    "data":{
        "note":"...",
        "code":200,
        "groupIds":[
            "group1"
        ],
        "faceIds":[
            "126041941642329***"
        ],
        "name":"易烊千玺",
        "personId":"person1"
    },
    "requestId":"6F3853D2-7E83-42B9-8014-420471B896CB"
}