Java SDK guide

更新时间:
复制 MD 格式

The IoT Platform Java SDK helps developers manage the platform programmatically using Java. You can add the SDK as a Maven dependency or download and install the package locally.

Install the SDK

  1. Install the Java development environment.

    Download the Java development environment from the official Java website and follow the instructions to install it.

  2. Install the IoT Java SDK.

    1. Go to the official Apache Maven website to download the Maven software.

    2. Add the Maven project dependency.

      • Maven dependency coordinates for the IoT Java SDK:

        <!-- 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>
      • Maven dependency coordinates for the Alibaba Cloud Java SDK common package:

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

Initialize the SDK

First, create an `IClientProfile` object named `profile` to store the SDK initialization parameters. Then, create a `DefaultAcsClient` instance named `client`. Call the `DefaultAcsClient(profile)` method to load the parameters from `profile` and initialize the SDK client.

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 the SDK client.

Parameter

Description

profile

The `profile` object stores the SDK initialization information. `${RegionId}` is the region code for your IoT Platform service.

View the region of the current service in the upper-left corner of the IoT Platform console.

For more information about region codes, see Regions and zones.

The following code shows an example of how to initialize the SDK to call an API 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 the SDK client.

Make a call

The IoT Platform cloud SDK encapsulates two classes for each API: `${API name}+"Request"` and `${API name}+"Response"`.

  • `${API name}+"Request"`: Represents an API request. You can create an instance of this class, named `request`, and call the `set+${Request parameter name}` method to set request parameters.

  • `${API name}+"Response"`: Represents the response to an API call. Call the `getAcsResponse(request)` method of the `DefaultAcsClient` instance `client` to retrieve a response. You can use an instance of this class, named `response`, and call the `get+${Response parameter name}` method to retrieve the values of the response parameters.

    For example, you can call `response.getSuccess()` to check if the call was successful. `Success` is a common response parameter. Other common parameters include `RequestId`, `ErrorMessage`, and `Code`.

For a list of IoT Platform cloud APIs, see API list. For descriptions of the request parameters in `request` and the response parameters in `response`, see the corresponding API reference.

This topic uses the Pub operation as an example to show how to publish a message to a topic. For information about the request parameters, see Pub.

Important

In the following code, ${iotInstanceId} represents the instance ID. You can find the ID of the current instance on the Instance Overview page in the IoT Platform console.

  • If an ID exists, you must pass this ID. Otherwise, the API call fails.

  • If the Instance Details page or the ID does not exist, you do not need to pass an ID. You can delete the request code related to `IotInstanceId` or pass an empty value `""`. Otherwise, the API call fails.

For more information about instances, see Instance overview. For information about how to purchase an instance, see Purchase an Enterprise instance. For 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 go to the IoT Platform Cloud SDK Example Center to view or download sample code for API calls. The sample code provides examples for the Java, Python, PHP, .NET, and Go SDKs.

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.