文档

查询指定相似图库信息

本文介绍了如何使用Java SDK查询指定相似图库信息。

功能描述

调用该接口查询指定相似图库信息。关于参数的详细说明,请参见查询指定相似图库API文档

您需要使用内容安全的API接入地址,调用本SDK接口。关于API接入地址的信息,请参见接入地址(Endpoint)

前提条件

  • 安装Java依赖。关于安装Java依赖的具体操作,请参见安装Java依赖

    说明

    请一定按照安装Java依赖页面中的版本安装,否则会导致调用失败。

  • 如果使用本地文件或者二进制文件检测,请下载并在项目工程中引入Extension.Uploader工具类

查询指定相似图库任务

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.green.model.v20180509.GetSimilarityLibraryRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;


public class GetSimilarityLibrarySample {

    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");
        IAcsClient client = new DefaultAcsClient(profile);

        GetSimilarityLibraryRequest request = new GetSimilarityLibraryRequest();
        // 指定API返回格式、请求方法。
        request.setAcceptFormat(FormatType.JSON);
        request.setMethod(MethodType.POST);
        request.setEncoding("utf-8");
        request.setProtocol(ProtocolType.HTTP);

        JSONObject httpBody = new JSONObject();
        httpBody.put("name", "相似图样本图库名称");

        request.setHttpContent(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(httpBody.toJSONString()),
                "UTF-8", FormatType.JSON);

        /**
         * 请设置超时时间,服务端全链路处理超时时间为10秒,请做相应设置。
         * 如果您设置的ReadTimeout小于服务端处理的时间,程序中会获得一个ReadTimeout异常。
         */
        request.setConnectTimeout(3000);
        request.setReadTimeout(10000);
        HttpResponse httpResponse = null;
        try {
            httpResponse = client.doAction(request);
            // 服务端接收到请求,完成处理后返回的结果。
            if (httpResponse != null && httpResponse.isSuccess()) {
                JSONObject scrResponse = JSON.parseObject(org.apache.commons.codec.binary.StringUtils.newStringUtf8(httpResponse.getHttpContent()));
                System.out.println(JSON.toJSONString(scrResponse, true));
                int requestCode = scrResponse.getIntValue("code");
                JSONObject data = scrResponse.getJSONObject("data");
                if (200 == requestCode) {
                    // 创建图库的时间戳。
                    int createTime = data.getIntValue("createTime");
                    // 图库中的图片样本的数量。
                    int imageCount = data.getIntValue("imageCount");
                    // 图库名称。
                    String name = data.getString("name");
                    // 图库所属区域。
                    String region = data.getString("region");
                } else {
                    /**
                     * 表明请求整体处理失败,原因视具体的情况详细分析。
                     */
                    System.out.println("the whole request failed. response:" + JSON.toJSONString(scrResponse));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  • 本页导读 (0)
文档反馈