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

Install the SDK

  1. Install Java.
    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 Maven dependencies.
      • Maven coordinates of 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.20.0</version>
        </dependency>
      • Maven coordinates of the core 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

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 go to the User Management console to create or view your AccessKey ID.

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

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 a class for each API operation. The class name is in the ${Operation name}+"Request" format. For information about the API operations of IoT Platform, see List of operations by function.

The following example shows how to call the Pub operation to publish a message to a topic.

For information about how to set request parameters in the request, see the corresponding API documentation. In this example, see Pub.

PubRequest request = new PubRequest(); 
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.getErrorMessage());
} 
catch (ServerException e) 
{
   e.printStackTrace();
}
 catch (ClientException e)
{
   e.printStackTrace();
}

Appendix: Sample code

Download the SDK demo. The demo includes the sample code for Java, Python, PHP, .NET, and Go.

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 the sample code of API operations 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.