本示例介绍如何使用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.aliyun/aliyun-java-sdk-green -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-green</artifactId>
            <version>3.5.1</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;

import java.util.ArrayList;
import java.util.List;

public class JianCe {

    public static void main(String[] args) throws Exception {
        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/video/results");
        // 指定请求版本
        request.setSysVersion("2018-05-09");
        // 支持HTTP和HTTPS
        request.setSysProtocol(ProtocolType.HTTPS);

        List<String> taskLists = new ArrayList<String>();
        // 这里添加要查询的taskId,提交任务的时候需要自行保存taskId
        taskLists.add("vi1qdy*******pE");
        request.setHttpContent(JSON.toJSONString(taskLists).getBytes("UTF-8"), "UTF-8", FormatType.JSON);

        // 请务必设置超时时间
        request.setSysConnectTimeout(3000);
        request.setSysReadTimeout(6000);
        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());
        }
    }
}

执行结果

正确额返回结果类似如下:
{
	"msg":"OK",
	"code":200,
	"data":[
		{
			"msg":"OK",
			"code":200,
			"dataId":"14574ce8-a71e-4fc4-b70e-25082692f320",
			"results":[
				{
					"rate":99.9,
					"suggestion":"pass",
					"label":"normal",
					"scene":"porn"
				},
				{
					"rate":99.9,
					"suggestion":"pass",
					"label":"normal",
					"scene":"terrorism"
				}
			],
			"taskId":"vipZQrsXwXtU6Zseqpv49A2-1rGq0g",
			"url":"http://aligreen-misc************3D"
		}
	],
	"requestId":"50BCC904-2DB1-4048-B729-90BC3C1C43E7"
}