全部产品

Java SDK

更新时间:2019-10-12 17:36:34

1. 准备工作


在安装和使用阿里云SDK前,确保您已经注册阿里云账号并生成访问访问密钥(AppKey)。


2. 将 SDK添加到项目中


1)pom.xml

  1. <dependency>
  2. <groupId>com.aliyun</groupId>
  3. <artifactId>aliyun-java-sdk-core</artifactId>
  4. <version>3.5.1</version>
  5. </dependency>


2、CreateTask

  1. import com.aliyuncs.CommonRequest;
  2. import com.aliyuncs.CommonResponse;
  3. import com.aliyuncs.DefaultAcsClient;
  4. import com.aliyuncs.exceptions.ClientException;
  5. import com.aliyuncs.http.MethodType;
  6. import com.aliyuncs.profile.DefaultProfile;
  7. public class Demo1 {
  8. //DefaultProfile.getProfile的参数分别是地域,access_key_id, access_key_secret
  9. public static DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LT********", "CB********");
  10. // public static DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "", "");
  11. public static DefaultAcsClient client = new DefaultAcsClient(profile);
  12. public static void main(String[] args) throws ClientException {
  13. CommonRequest request = new CommonRequest();
  14. request.setMethod(MethodType.POST);
  15. request.setDomain("retailir.cn-hangzhou.aliyuncs.com");
  16. request.setVersion("2019-09-06");
  17. request.setAction("CreateTask");
  18. request.putBodyParameter("Appkey", "71********");
  19. request.putBodyParameter("TaskName", "demotest1");
  20. request.putBodyParameter("ImageLeftTop", "https://img.alicdn.com/tfs/TB1OFfegEY1gK0jSZFCXXcwqXXa-720-1440.png");
  21. CommonResponse response = null;
  22. try {
  23. response = client.getCommonResponse(request);
  24. } catch (ClientException e) {
  25. e.printStackTrace();
  26. }
  27. System.out.println(response.getData());
  28. }
  29. }


3、GetTask

  1. import com.aliyuncs.CommonRequest;
  2. import com.aliyuncs.CommonResponse;
  3. import com.aliyuncs.DefaultAcsClient;
  4. import com.aliyuncs.exceptions.ClientException;
  5. import com.aliyuncs.http.MethodType;
  6. import com.aliyuncs.profile.DefaultProfile;
  7. public class Demo2 {
  8. //DefaultProfile.getProfile的参数分别是地域,access_key_id, access_key_secret
  9. public static DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LT********", "CB********");
  10. public static DefaultAcsClient client = new DefaultAcsClient(profile);
  11. public static void main(String[] args) throws ClientException {
  12. CommonRequest request = new CommonRequest();
  13. request.setMethod(MethodType.POST);
  14. request.setDomain("retailir.cn-hangzhou.aliyuncs.com");
  15. request.setVersion("2019-09-06");
  16. request.setAction("GetTask");
  17. request.putBodyParameter("Appkey", "71********");
  18. request.putBodyParameter("JobId", "cb89d9cd4ae043bc835797708e4ce92a");
  19. CommonResponse response = null;
  20. try {
  21. response = client.getCommonResponse(request);
  22. } catch (ClientException e) {
  23. e.printStackTrace();
  24. }
  25. System.out.println(response.getData());
  26. }
  27. }