All Products
Search
Document Center

IoT Platform:Use IoT Platform SDK for Java

Last Updated:Oct 22, 2023

IoT Platform SDK for Java allows you to efficiently manage IoT Platform resources by using Java programs. You can add the SDK as a dependency to your Maven project. You can also download the installation package to a local directory and install the SDK.

Install the SDK

  1. Install a Java development environment.

    Download a Java installation package from the official Java website and install Java as prompted.

  2. Install IoT Platform SDK for Java.

    1. Download a Maven installation package from the official Apache Maven website.

    2. Add the following dependencies to your Maven project:

      • Dependency for IoT Platform SDK for Java of the latest version:

        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-iot -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-iot</artifactId>
            <version>7.41.0</version>
        </dependency>
      • Dependency for Alibaba Cloud SDK for Java:

        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.5.6</version>
        </dependency>

Initialize the SDK

Create a profile object of the IClientProfile class to store SDK initialization information. Then, create a client instance of the DefaultAcsClient class. Call the DefaultAcsClient(profile) method to load the SDK initialization information.

String accessKey = System.getenv("ACCESS_KEY_ID");
String accessSecret = System.getenv("ACCESS_KEY_SECRET");
IClientProfile profile = DefaultProfile.getProfile("${RegionId}", accessKey, accessSecret);
DefaultAcsClient client = new DefaultAcsClient(profile); // Initialize an SDK client.

Parameter

Description

profile

The object that is used to store the SDK initialization information. ${RegionId} indicates the ID of the region in which your IoT Platform service resides.

You can view the region in the upper-left corner of the IoT Platform console.

For more information about region IDs, see Regions and Zones.

The following example shows how to initialize the SDK if your IoT Platform service resides in the China (Shanghai) region:

String accessKey = System.getenv("ACCESS_KEY_ID");
String accessSecret = System.getenv("ACCESS_KEY_SECRET");
IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKey, accessSecret);
DefaultAcsClient client = new DefaultAcsClient(profile); // Initialize an SDK client.

Initiate a request

The SDK encapsulates two classes for each API operation. The class names are in the ${API operation name}+"Request" and ${API operation name}+"Response" formats.

  • ${API operation name}+"Request": This class is used to call an API operation. You can create a request instance of this class and call the set +${request parameter} method to specify the request parameters.

  • ${API operation name}+"Response": You can call the getAcsResponse(request) method of the client instance that is created from DefaultAcsClient to obtain a response. You can call the get +${response parameter} method of the response instance of the ${API operation name}+"Response" class to obtain the values of the response parameters.

    For example, you can call the response.getSuccess() method to obtain the Success parameter. This parameter is a common response parameter that indicates whether a request is successful. Common response parameters also include RequestId, ErrorMessage, and Code.

For more information about the API operations of IoT Platform, see List of operations by function. For more information about the request and response parameters of each API operation, see the API documentation.

The following example shows how to call the Pub operation to publish a message to a topic. For more information about the request parameters, see Pub.

Important

In the following sample code, ${iotInstanceId} specifies the ID of an instance. You can view the ID of the instance on the Overview page in the IoT Platform console.

  • If your instance has an ID, you must specify the ID for this parameter. Otherwise, the request fails.

  • If no Overview page or ID is generated for your instance, you do not need to specify this parameter. You must delete the request code that is related to the IotInstanceId parameter or specify an empty string ("") for the parameter. Otherwise, the request fails.

For more information about IoT Platform instances, see Overview. For more information about how to purchase an instance, see Purchase Enterprise Edition instances. For more information about frequently asked questions, see FAQ about IoT Platform instances.

PubRequest request = new PubRequest(); 
request.setIotInstanceId("${iotInstanceId}"); 
request.setProductKey("${productKey}"); 
request.setMessageContent(Base64.encodeBase64String("hello world".getBytes())); 
request.setTopicFullName("/${productKey}/${deviceName}/user/get"); 
request.setQos(0); // QoS 0 and QoS 1 are supported.  
try 
{ 
   PubResponse response = client.getAcsResponse(request); 
   System.out.println(response.getSuccess()); 
   System.out.println(response.getCode());
   System.out.println(response.getErrorMessage());
} 
catch (ServerException e) 
{
   e.printStackTrace();
}
catch (ClientException e)
{
   System.out.println("ErrCode:" + e.getErrCode());
   System.out.println("ErrMsg:" + e.getErrMsg());
   e.printStackTrace();
}

Appendix: Sample code

You can view or download the sample code of API operations in IoT Platform SDK Sample Center. The sample code of the SDKs for Java, Python, PHP, .NET, and Go is provided.

Alibaba Cloud OpenAPI Explorer provides online debugging tools for API operations. On the API Debugging page, you can search for API operations, call API operations, and generate sample code for API operations of different SDKs. On the right side of the page, you can view the sample code of an SDK on the Sample Code tab. On the Debugging Result tab, you can view the actual request URL and response in the JSON format.