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 at the Android Studio official website and install it.
Obtaining the SDK
Log in to the API Gateway console and choose Help Center > SDK Process Flow. Then download the SDK. For details, see section "SDKs" in the User Guide.
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
- Start Android Studio and choose File > Open.
Select the directory where the SDK is decompressed.
- View the directory structure of the project shown in the following figure.
Request Signing and API Calling
- 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
- 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' }
- Create a request, enter the AK and SK, and specify the domain name, method, request URI, and body.
1 2 3 4 5 6 7 8 9 10 11 12 13
Request request = new Request(); try { request.setKey("QTWAOYTT*******KYUC"); request.setSecrect("MFyfvK41ba2giqM7**********KGpownRZlmVmHc"); 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; }
- 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();
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.