本示例介绍使用Alibaba Cloud SDK for Java调用QueryDeviceListByDeviceGroup接口查询分组中设备列表。

前提条件

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

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-iot</artifactId>
    <version>6.11.0</version>
 </dependency>

示例代码

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

/**
 * 调用该接口查询分组中的设备列表。
 */
public class TestQueryDeviceListByDeviceGroup {

    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);
        // 创建请求接口
        QueryDeviceListByDeviceGroupRequest request = new QueryDeviceListByDeviceGroupRequest();
        // 分组ID,分组的全局唯一标识符。
        request.setGroupId("yourGroupId");
        request.setCurrentPage(1);
        request.setPageSize(10);

        try {
            // 发起请求并获取返回值
            QueryDeviceListByDeviceGroupResponse 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());
        }
    }
}
说明 更多参数信息,请参见APi参考