The IoT Java SDK allows you to create Java applications to manage IoT Platform resources with ease. You can add the SDK as a Maven dependency to your project, or download the installation package to a local directory and install the SDK.

Install the IoT Java SDK

  1. Install a Java development environment.

    You can download a Java IDE from Java official website, and install the IDE by following the instructions.

  2. Install the IoT Java SDK.
    1. Visit Apache Maven official website to download a Maven installation package.
    2. Add a Maven dependency.
      Maven coordinates of the IoT Java SDK dependency
      <! -- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-iot -->
      <dependency>
          <groupId>com.aliyun</groupId>
          <artifactId>aliyun-java-sdk-iot</artifactId>
          <version>7.7.0</version>
      </dependency>
      Public libraries
      <dependency>
          <groupId>com.aliyun</groupId>
          <artifactId>aliyun-java-sdk-core</artifactId>
          <version>3.5.1</version>
      </dependency>

Initialize the SDK

Note The following example uses the China (Shanghai) region and the corresponding endpoint. You must specify the region of your IoT Platform and the endpoint of your service to use the SDK.
String accessKey = "<your accessKey>";
String accessSecret = "<your accessSecret>";
DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Iot", "iot.cn-shanghai.aliyuncs.com");
IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKey, accessSecret);
DefaultAcsClient client = new DefaultAcsClient(profile); //Initialize the SDK

accessKeyId is the AccessKeyId of your account, and accessSecret is the AccessKeySecret paired with the AccessKeyId. You can go to AccessKey page in the console to create or view your AccessKey.

Initiate a call

The following example demonstrates how to call the Pub operation to publish messages to a topic.

PubRequest request = new PubRequest(); 
request.setProductKey("productKey"); 
request.setMessageContent(Base64.encodeBase64String("hello world".getBytes())); 
request.setTopicFullName("/productKey/deviceName/get"); 
request.setQos(0); //support QoS0 and QoS1 
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

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 real request URL and response in the JSON format.