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.

Preparing the Environment

  • You have obtained the domain name, request URL, and request method of the API to be called, and the AppKey and AppSecret of the App for calling the API. For more information, see Preparation.
  • You have installed Android Studio. If not, download Android Studio from the official Android Studio website and install it.

Obtaining the SDK

Download the SDK at "https://apig.${Domain name of OBS}/apig-sdk/ApiGateway-android-sdk.zip".

Replace ${Domain name of OBS} with the actual domain name of OBS obtained from the administrator.

Download ApiGateway-android-sdk.zip. 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.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 an AppKey and AppSecret, and specify the domain name, method, request URI, and body. The ID of the accessed API is mandatory. You need to enter the specific ID information. x-api-id is used in the sample code.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    Request request = new Request();
    try {
    	request.setKey("4f5f626b-073f-402f-a1e0-e52171c6100c");
    	request.setSecrect("******");
    	request.setMethod("POST");
    	request.setUrl("https://c967a237-cd6c-470e-906f-a8655461897e.apigw.cn-north-1.huaweicloud.comserviceEndpoint/app1");
    	request.addQueryStringParam("name", "value");
    	request.addHeader("Content-Type", "text/plain");
            request.addHeader("x-api-id", "*********");
    	request.setBody("demo");
    } catch (Exception e) {
    	e.printStackTrace();
    	return;
    }
    

  4. Sign the request and add the x-Authorization header to the request. The value of the x-Authorization header is the same as that of the Authorization header. The okhttp3.Request object is then generated to access the API.

    1
    2
    3
    4
    5
    okhttp3.Request signedRequest = Client.signOkhttp(request);
    String authorization = signedRequest.header("Authorization");
    signedRequest = signedRequest.newBuilder().addHeader("x-Authorization",authorization).build();
    OkHttpClient client = new OkHttpClient.Builder().build();
    Response response = client.newCall(signedRequest).execute();