Updated on 2024-06-27 GMT+08:00

Android

This section uses Android Studio as an example to describe how to integrate the Android SDK for API request signing. You can import the sample project in the code package, and integrate the signing SDK into your application by referring to the API calling example.

Preparing the Environment

Download Android Studio 4.1.2 or later at the Android Studio official website and install it.

Obtaining the SDK

Download the SDK and demo.

The following table shows the directory structure of the downloaded package.

Name

Description

app\

Android project code

build.gradle

Gradle configuration files

gradle.properties

settings.gradle

Opening the Sample Project

  1. Start Android Studio and choose File > Open.

    Select the directory where the SDK is decompressed.

  2. View the directory structure of the project shown in the following figure.

Request Signing and API Calling

  1. Add required JAR files to the app/libs directory of the Android project. The following JAR files must be included:

    • java-sdk-core-x.x.x.jar
    • commons-logging-1.2.jar
    • joda-time-2.9.9.jar

  2. Add dependencies of the okhttp library to the build.gradle file.

    Add implementation 'com.squareup.okhttp3:okhttp:3.11.0' in the dependencies field of the build.gradle file.
    1
    2
    3
    4
    5
    dependencies {    
        ...
        ...
        implementation 'com.squareup.okhttp3:okhttp:3.11.0'
    }
    

  3. Create a request, enter the AK and SK, and specify the domain name, method, request URI, and body.

    1. In this example, the AK and SK stored in the environment variables are used. Specify the environment variables HUAWEICLOUD_SDK_AK and HUAWEICLOUD_SDK_SK in the local environment first. The following uses Linux as an example to describe how to set the obtained AK/SK as environment variables.
      1. Open the terminal and run the following command to open the environment variable configuration file:

        vi ~/.bashrc

      2. Set environment variables, save the file, and exit the editor.
        export HUAWEICLOUD_SDK_AK="Obtained AK"
        export HUAWEICLOUD_SDK_SK="Obtained SK"
      3. Run the following command to apply the modification:

        source ~/.bashrc

    2. Create a request, enter the configured environment variables, and specify the domain name, method, request URI, and body.
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      Request request = new Request();
      try {
      // Directly writing AK/SK in code is risky. For security, encrypt your AK/SK and store them in the configuration file or environment variables.
      // In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, set environment variables HUAWEICLOUD_SDK_AK and HUAWEICLOUD_SDK_SK.
              request.setKey(System.getenv("HUAWEICLOUD_SDK_AK"));   
              request.setSecret(System.getenv("HUAWEICLOUD_SDK_SK"));
              request.setMethod("GET");
              request.setUrl("https://service.region.example.com3/v1/{project_id}/vpcs");
              request.addQueryStringParam("name", "value");
              request.addHeader("Content-Type", "text/plain");
              //request.setBody("demo");
      } catch (Exception e) {
      	e.printStackTrace();
      	return;
      }
      

  4. Sign the request to generate an okhttp3.Request object for API access.

    1
    2
    3
    okhttp3.Request signedRequest = Client.signOkhttp(request);
    OkHttpClient client = new OkHttpClient.Builder().build();
    Response response = client.newCall(signedRequest).execute();