Java Demo

This topic describes how to use the sample code (Java) for calling APIs. For details on these APIs, see API Reference on the Application Side.

(Optional) Preparing the Java Development Environment

If you have prepared the Java development environment, skip this section.

This section describes how to install the JDK 1.8 and Eclipse in the Windows operating system. If you use another development environment, deploy the two tools based on project situations.

  1. Download JDK 1.8 (for example, jdk-8u161-windows-x64.exe) from the Java JDK website, and double-click it to install it.
  2. Configure Java environment variables.

    1. Right-click Computer and choose Properties.

    2. Select Advanced system settings.

    3. In the System Properties dialog box, choose Advanced > Environment Variables.

    4. Configure the system variables. Configure the following three variables: JAVA_HOME, Path, and CLASSPATH (where the variable names are case-insensitive). If a variable name already exits, click Edit. If a variable name does not exist, click New to create one. Generally, the Path variable exists, and the JAVA_HOME and CLASSPATH variables need to be added.

      JAVA_HOME indicates the JDK installation path and is set to C:\ProgramFiles\Java\jdk1.8.0_45. This path contains the lib and bin files.

      Path enables the system to recognize a Java command in any path. If the Path variable exists, add a path at the end of the variable value. Configuration example: ;C:\Program Files\Java\jdk1.8.0_45\bin;C:\Program Files\Java\jdk1.8.0_45\jre\bin

      Separate two paths using a semicolon (;).

      CLASSPATH specifies the path of loaded Java classes (class or lib). Java commands can be identified only if they are contained in the class path. Configuration example: .;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar

      Note: The path starts with a dot (.), indicating the current path.

    5. Choose Start > Run, enter cmd, and run the following commands: Java -version, java, and javac. If the commands can be run, the environment variables are set.

  3. Download the Eclipse installation package from the IDEA website and decompress it to the local directory.

Importing the Demo Project

This section describes how to call APIs based on the Java sample code. Do not use the sample code for commercial use. For details on these APIs, see API Reference on the Application Side.

  1. Download and decompress the API demo in Java.
  2. Open IDEA, click Import Project, select pom.xml in the decompressed demo folder, and click OK.

  3. Choose File > Setting > Build, Execution, Deployment > Build Tools > Maven, set User setting file to the path of the settings.xml file of Maven, and set Local repository to the path of the local Maven repository.

Obtaining a Token

Before accessing platform APIs, an application must call the API Obtaining the Token of an IAM User for authentication. After the authentication is successful, HUAWEI CLOUD returns the authentication token X-Subject-Token to the application.

This section describes how to call the authentication API based on the Java code sample of the API.

  1. In IDEA, choose JavaApiDemo > src > main > java > com.huawei,.util > Constants.java, and then change the values of TOKEN_BASE_URL and IOTDM_BASE_URL.

    Parameters are described as follows:

    • TOKEN_BASE_URL: Enter the address for interconnecting with IAM, that is, the IAM endpoint, which can be obtained from IAM Regions and Endpoints.
    • IOTDM_BASE_URL: Enter the address for interconnecting with IoTDA, that is, the IoTDA endpoint, which can be obtained from IoTDA Regions and Endpoints.

      The endpoints vary depending on the region. Obtain the endpoints based on project conditions. For example, if you have subscribed to IoTDA in CN North-Beijing 4, obtain the endpoint of CN North-Beijing 4 from IoTDA Regions and Endpoints.

  2. In the imported sample code, choose JavaApiDemo > src > main > java > com.huawei.demo.auth > Authentication.java.

    Change the account information to your own account information, right-click Authentication.java, and choose Run Authentication.main() to run the code.

  3. View the response log on the console. If a token is obtained, the authentication is successful.

    Keep the token secure. It will be used when you call other APIs.

    If no correct response is obtained, check whether the global constants are modified correctly or whether a network fault occurs.

    Note: For each attempt to obtain a new token, the system preferentially retrieves the existing token stored in the file. If the token has expired, the system deletes the token.text file and obtains a new one.

Device Registration (Token Authentication)

Before connecting a device to the platform, an application must call the API Creating a Device. Each device connecting to the platform carries the device ID to complete access authentication. For details, see API Reference.

This section describes how to call the API based on the Java sample code of the API.

  1. In IDEA, choose JavaApiDemo > src > main > java > com.huawei.demo.device > CreateDevice.java.

    Modify parameters such as nodeId, timeout, secret, deviceName, and productId. For details on the parameter description, see the API Creating a Device.

    Add the obtained token to the X-Auth-Token request header.

  2. In IDEA, right-click CreateDevice.java and choose Run CreateDevice.main() to run the code.
  3. View the response log on the console. If all types of subscriptions obtain the response "201" as well as deviceId, the subscription is successful.

Device Query (Token Authentication)

Applications can call the API Querying a Device to query details about a device registered with the platform.

This section describes how to call the API based on the Java code sample of the API.

  1. In IDEA, choose JavaApiDemo > src > main > java > com.huawei.demo.device > QueryDeviceList.java, and then modify the corresponding parameters.

  2. Right-click QueryDeviceList and choose Run QueryDeviceList.main() to run the code.
  3. View the response log on the console. If deviceId is obtained, the query is successful.

Device Registration (AK/SK Authentication)

In addition to token authentication, AK/SK authentication is supported for calling platform APIs. This section describes how to call the AK/SK authentication API based on the sample code (Java) for calling APIs.

  1. In IDEA, choose JavaApiDemo > src > main > java > com.huawei.demo.device > CreateDeviceByAK.java, modify the corresponding parameters, and call the SignUtil.signRequest() method to sign the request.

  2. In IDEA, choose JavaApiDemo > src > main > java > com.huawei.demo.apig > SignUtil.java, and modify the AK/SK in the signRequest() method. For details, see Obtaining an AK/SK.

  3. In IDEA, right-click CreateDeviceByAK.java and choose Run CreateDeviceByAK.main() to run the code.
  4. View the response log on the console. If all types of subscriptions obtain the response "201" as well as deviceId, the subscription is successful.

Device Query (AK/SK Authentication)

Applications can call the API Querying a Device to query details about a device registered with the platform.

This section describes how to call the API based on the Java code sample of the API.

  1. In IDEA, choose JavaApiDemo > src > main > java > com.huawei.demo.device > QueryDeviceListByAK.java, modify the corresponding parameters, sign the request, and replace the AK/SK in the signature method. For details, see Obtaining an AK/SK.

  2. Right-click QueryDeviceListByAK and choose Run QueryDeviceListByAK.main() to run the code.
  3. View the response log on the console. If deviceId is obtained, the query is successful.

Development of Other APIs

For details on how to develop other APIs, see API Reference.

Performing Single-Step Debugging

To intuitively view requests sent by applications and responses from the platform, use the breakpoint debugging method of IDEA.

  1. Set breakpoints in the code where HTTP or HTTPS messages are sent. For example, set three breakpoints for the execute method in the sample code HttpsUtil.java. (Set the breakpoints based on your actual code.)

  2. Right-click the class to debug, for example, CreateDevice.java, and choose Debug > CreateDevice.main().
  3. After the program stops running at the breakpoint, click Step Over to perform single-step debugging. You can view the content of the variables in the Variables window, such as the request and response.

  4. Expand the request variable in the Variables window to view the content.

    1. When the request variable is selected, the URL of the request sent by the application is displayed in the uri area, and the content of the request is displayed in the entity area.

    2. The token is carried in headerGroup.

  5. Expand the response variable in the Variables window to view the content.

    In the sample code, all classes other than Authentication.java call the Authentication API in the first step. Therefore, if you want to obtain a new token during single-step debugging on a class other than Authentication.java, view the variable content when the program reaches the breakpoint for the second time.