本示例介绍使用Alibaba Cloud SDK for Java调用CreateDeviceGroup接口创建分组。

前提条件

请在pom.xml文件中增加以下依赖,准确的SDK版本号,参见阿里云开发工具包(SDK)

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>java.demo</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <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-iot -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-iot</artifactId>
            <version>6.11.0</version>
        </dependency>
    </dependencies>
</project>

示例代码

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.iot.model.v20180120.CreateDeviceGroupRequest;
import com.aliyuncs.iot.model.v20180120.CreateDeviceGroupResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;

/**
 * 调用接口创建分组
 */
public class TestCreateDeviceGroup {
    public static void main(String[] args)  {
        DefaultProfile profile = DefaultProfile.getProfile(
                 "<your-region-id>",            // 地域ID
                 "<your-access-key-id>",        // 您的AccessKey ID
                 "<your-access-key-secret>");  // 您的AccessKey Secret
        IAcsClient client = new DefaultAcsClient(profile);
        // 创建请求接口
        CreateDeviceGroupRequest request = new CreateDeviceGroupRequest();
        //分组名称。名称可包含中文汉字、英文字母、数字和下划线(_)。长度范围 4 - 30 字符(一个中文汉字占二个字符)。
        request.setGroupName("测试设备组");
        //分组描述。长度限制为100字符(一个中文汉字占一个字符)。
        request.setGroupDesc("测试设备组的描述");
        // 父组ID。
        // 若要创建的是一级分组,则不传入此参数。
        // request.setSuperGroupId("");

        try {
            CreateDeviceGroupResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } 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());
        }
    }
}