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 as well as the key and secret (or AppKey and AppSecret of the client) of the integration application. For details, see Preparations.
- You have installed Android Studio 4.1.2 or later. If not, download Android Studio from the Android Studio official website and install it.
Obtaining the SDK
Log in to the ROMA Connect console, choose API Connect > API Calling, and download the SDK. The directory structure after the decompression is as follows:
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 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.
Figure 1 Project directory structure
API Calling Example
- 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
- joda-time-2.10.jar
- 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' }
- Create a request, enter an AppKey and AppSecret, and specify the domain name, method, request URI, and body.
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.exampleRegion.com/app1"); 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.
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.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot