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 integrated development environment (IDE).
    Download a Java installation package from the official Java website and follow the instructions to install Java.
  2. Install IoT Platform SDK for Java.
    1. Visit the official Apache Maven website to download a Maven installation package.
    2. Add the following dependencies to your Maven project:
      • The dependency for the latest version of IoT Platform SDK for Java:
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-iot -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-iot</artifactId>
            <version>7.38.0</version>
        </dependency>
      • The 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 the profile object from IClientProfile to store the SDK initialization information. Then, create the client instance from DefaultAcsClient. Use the DefaultAcsClient(profile) method to load the SDK initialization information.

String accessKey = "${accessKey}";
String accessSecret = "${accessSecret}";
IClientProfile profile = DefaultProfile.getProfile("${RegionId}", accessKey, accessSecret);
DefaultAcsClient client = new DefaultAcsClient(profile); // Initialize an SDK client. 
Parameter Description
accessKey The AccessKey ID of your Alibaba Cloud account.

You can create or view your AccessKey pair in the Alibaba Cloud Management Console.

accessSecret The AccessKey secret of your Alibaba Cloud account.
profile The object that is used to store the SDK initialization information. ${RegionId} specifies the ID of the region in which your IoT Platform service is deployed.

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 is deployed in the China (Shanghai) region:

String accessKey = "${accessKey}";
String accessSecret = "${accessSecret}";
IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKey, accessSecret);
DefaultAcsClient client = new DefaultAcsClient(profile); 

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 use the request instance of this class and call the set +${request parameters} method to configure 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 use the response instance of of the ${API operation name}+"Response" class and call the get +${response parameters} method to obtain the response parameters.

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

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 request parameters, see Pub.

Notice In the following code, iotInstanceId is the ID of the instance.

You can view the instance ID on the Overview page in the IoT Platform console.

  • If your instance has an ID, you must configure this parameter. If you do not set this parameter, the call fails.
  • If your instance has no Overview page or ID, you do not need to set this parameter.
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); // QoS0 and QoS1 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

Download the IoT Platform SDK sample code. The sample code for Java, Python, PHP, .NET, and Go is provided.

Alibaba Cloud provides OpenAPI Explorer to simplify API usage. You can use OpenAPI Explorer to search for API operations, call API operations, and dynamically generate SDK sample code. OpenAPI Explorer dynamically generates the sample code of the operation for different SDKs. On the right side of the page, you can view the sample code of an SDK on the Example Code tab. On the Debugging Result tab, you can view the actual request URL and response in the JSON format.