Updated on 2024-03-28 GMT+08:00

Application Java SDK

IoTDA provides an application SDK in Java for developers. This topic describes how to install and configure the Java SDK and how to use it to call application-side APIs.

Obtaining and Installing the SDK

  1. Install the Java development environment.

    Visit the Java website, and download and install the Java development environment.

    The Java SDK can be used in Java JDK 1.8 or later.

  2. Install Maven.

    Download and install Maven. After Maven is installed, add the dependencies to the pom.xml file of the Java project.

  3. Install the Java SDK.

    Add Maven dependencies.

    <dependency>
        <groupId>com.huaweicloud.sdk</groupId>
        <artifactId>huaweicloud-sdk-core</artifactId>
        <version>[3.0.40-rc, 3.2.0)</version>
    </dependency>
    <dependency>
        <groupId>com.huaweicloud.sdk</groupId>
        <artifactId>huaweicloud-sdk-iotda</artifactId>
        <version>[3.0.40-rc, 3.2.0)</version>
    </dependency>

Code Sample

Use a version range for the Maven dependency versions. If you use a specific version, use 3.0.60 or later.

The following code sample shows how to use the Java SDK to call API Querying the Device List.

  1. Create a credential.
  2. Create and initialize an IoTDAClient instance.
  3. Instantiate a request object.
  4. Call the API for querying the device list.

    package com.huaweicloud.sdk.test;
    
    import com.huaweicloud.sdk.core.auth.ICredential;
    import com.huaweicloud.sdk.core.exception.ConnectionException;
    import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
    import com.huaweicloud.sdk.core.exception.ServiceResponseException;
    import com.huaweicloud.sdk.core.region.Region;
    import com.huaweicloud.sdk.core.auth.BasicCredentials;
    import com.huaweicloud.sdk.iotda.v5.*;
    import com.huaweicloud.sdk.iotda.v5.model.*;
    
    public class ListDevicesSolution {
    
        // REGION_ID: If CN East-Shanghai1 is used, enter cn-east-3. If CN North-Beijing4 is used, enter cn-north-4. If CN South-Guangzhou is used, enter cn-south-1.
        private static final String REGION_ID = "<YOUR REGION ID>";
        //ENDPOINT: On the console, choose Overview and click Access Addresses to view the HTTPS application access address.
        private static final String ENDPOINT = "<YOUR ENDPOINT>";
    
        public static void main(String[] args) {
            // There will be security risks if the AK/SK used for authentication is directly written into code. Encrypt the AK/SK in the configuration file or environment variables for storage;
            // In this example, the AK/SK stored in the environment variables are used. Configure the environment variables HUAWEICLOUD_SDK_AK and HUAWEICLOUD_SDK_SK in the local environment first.
            String ak = System.getenv("HUAWEICLOUD_SDK_AK");        
            String sk = System.getenv("HUAWEICLOUD_SDK_SK");
            String projectId = "<YOUR PROJECTID>";
    
            // Create a credential.
            ICredential auth = new BasicCredentials()
                   .withAk(ak)
                   .withSk(sk)
                   // WithDerivedPredicate is used for the standard or enterprise edition. For the basic edition, delete the line.
                   .withDerivedPredicate(BasicCredentials.DEFAULT_DERIVED_PREDICATE)
                   .withProjectId(projectId);
    
            // Create and initialize an IoTDAClient instance.
            IoTDAClient client = IoTDAClient.newBuilder()
                    .withCredential(auth)
                    // For the standard or enterprise edition, create a region object. For the basic edition, select the region object in IoTDARegion. For example, withRegion(IoTDARegion.CN_NORTH_4).
                    .withRegion(new Region(REGION_ID, ENDPOINT))
                   // .withRegion(IoTDARegion.CN_NORTH_4)
                    .build();
    
            // Instantiate a request object.
            ListDevicesRequest request = new ListDevicesRequest();
            try {
                // Call the API for querying the device list.
                ListDevicesResponse response = client.listDevices(request);
                System.out.println(response.toString());
            } catch (ConnectionException e) {
                e.printStackTrace();
            } catch (RequestTimeoutException e) {
                e.printStackTrace();
            } catch (ServiceResponseException e) {
                e.printStackTrace();
                System.out.println(e.getHttpStatusCode());
                System.out.println(e.getErrorCode());
                System.out.println(e.getErrorMsg());
            }
        }
    }

Parameter

Description

ak

Access key ID of your Huawei Cloud account. You can create and view an AK/SK on the My Credentials > Access Keys page of the Huawei Cloud management console. For more information, see Access Keys.

sk

Secret access key (SK) of your Huawei Cloud account.

projectId

Project ID. For details on how to obtain a project ID, see Obtaining a Project ID.

IoTDARegion.CN_NORTH_4

Region where the IoT platform to be accessed is located. The available regions of the IoT platform have been defined in the SDK code IoTDARegion.java.

On the console, you can view the region name of the current service and the mapping between regions and endpoints. For details, see Platform Connection Information.

REGION_ID

If CN East-Shanghai1 is used, enter cn-east-3. If CN North-Beijing4 is used, enter cn-north-4. If CN South-Guangzhou is used, enter cn-south-4.

ENDPOINT

On the console, choose Overview and click Access Addresses to view the HTTPS application access address.

For details on the project source code and usage guide, see Huawei Cloud Java Software Development Kit (Java SDK).