OneNet多协议设备API调用详解

OneNet多协议的文档地址,但是由于OneNet的业务升级,该文档以及不再升级修改了。

OneNET – 中国移动物联网开放平台 (10086.cn)

以调用《查询设备详情》api为例,以下代码为在Java中如何使用该api的demo:

public static void main(String[] args) throws IOException {
    //deviceID
    String deviceID = "";
    // 查询多协议设备详情的api
    String apiUrl = "https://api.heclouds.com/devices/";
    // 替换为你的 OneNet 多协议设备 API Key
    String apiKey = "";
    // 创建 URL 对象
    URL url = new URL(apiUrl+deviceID);
    // 打开连接
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    // 设置请求方法为 GET
    connection.setRequestMethod("GET");
    // 设置请求头部
    connection.setRequestProperty("api-key", apiKey);
    // 获取响应数据
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
        StringBuilder response = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        System.out.println("Response: " + response.toString());
    } catch (IOException e) {
        // 如果请求失败,则获取错误信息
        try (BufferedReader errorReader = new BufferedReader(new InputStreamReader(connection.getErrorStream()))) {
            StringBuilder errorResponse = new StringBuilder();
            String errorLine;
            while ((errorLine = errorReader.readLine()) != null) {
                errorResponse.append(errorLine);
            }
            System.out.println("Error Response: " + errorResponse.toString());
        }
    } finally {
        // 关闭连接
        connection.disconnect();
    }
}

上述代码就是api的基础使用,其他api逻辑无异,相同的调用即可。

作者:xiaolinzi625160

物联沃分享整理
物联沃-IOTWORD物联网 » OneNet多协议设备API调用详解

发表回复