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

Android

Scenarios

To use Android to call an API through app authentication, obtain the Android SDK, create a new project, and then call the API by referring to the API calling example.

Prerequisites

  • You have obtained API calling information. For details, see Preparation.
  • You have installed Android Studio 4.1.2 or a later version. If not, download it from the official Android Studio website and install it.

Obtaining the SDK

Log in to the APIG console, and download the SDK on the SDKs page by referring to section "SDKs" in the API Gateway User Guide.

Then obtain the ApiGateway-android-sdk.zip package. The following table shows the files decompressed from the package.

Name

Description

app\

Android project code

gradle\

Gradle files

build.gradle

Gradle configuration files

gradle.properties

settings.gradle

gradlew

Gradle Wrapper scripts

gradlew.bat

Opening a Project

  1. Start the 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.

    Figure 1 Project directory structure

API Calling Example

  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.10.jar

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

    Add implementation 'com.squareup.okhttp3:okhttp:3.14.2' in the dependencies field of the build.gradle file.
    dependencies {    
        ...
        ...
        implementation 'com.squareup.okhttp3:okhttp:3.14.3'
    }

  3. Create a request, enter an AppKey and AppSecret, 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 CLOUD_SDK_AK and CLOUD_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 CLOUD_SDK_AK="Obtained AK"
        export CLOUD_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.
      Request request = new Request();
      try {
              request.setKey(System.getenv("CLOUD_SDK_AK"));   
              request.setSecret(System.getenv("CLOUD_SDK_SK"));
      	request.setMethod("POST");
      	request.setUrl("https://c967a237-cd6c-470e-906f-a8655461897e.apigw.exampleRegion.com/app1");
      	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.

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