Use an Android SDK generated by API Gateway for a REST API - Amazon API Gateway

Use an Android SDK generated by API Gateway for a REST API

In this section, we will outline the steps to use an Android SDK generated by API Gateway for a REST API. Before proceeding further, you must have already completed the steps in Generate SDKs for REST APIs in API Gateway.

Note

The generated SDK is not compatible with Android 4.4 and earlier. For more information, see Amazon API Gateway important notes.

To install and use an Android SDK generated by API Gateway
  1. Extract the contents of the API Gateway-generated .zip file that you downloaded earlier.

  2. Download and install Apache Maven (preferably version 3.x).

  3. Download and install JDK 8.

  4. Set the JAVA_HOME environment variable.

  5. Run the mvn install command to install the compiled artifact files to your local Maven repository. This creates a target folder containing the compiled SDK library.

  6. Copy the SDK file (the name of which is derived from the Artifact Id and Artifact Version you specified when generating the SDK, e.g., simple-calcsdk-1.0.0.jar) from the target folder, along with all of the other libraries from the target/lib folder, into your project's lib folder.

    If you use Android Studio, create a libs folder under your client app module and copy the required .jar file into this folder. Verify that the dependencies section in the module's gradle file contains the following.

    compile fileTree(include: ['*.jar'], dir: 'libs') compile fileTree(include: ['*.jar'], dir: 'app/libs')

    Make sure no duplicated .jar files are declared.

  7. Use the ApiClientFactory class to initialize the API Gateway-generated SDK. For example:

    ApiClientFactory factory = new ApiClientFactory(); // Create an instance of your SDK. Here, 'SimpleCalcClient.java' is the compiled java class for the SDK generated by API Gateway. final SimpleCalcClient client = factory.build(SimpleCalcClient.class); // Invoke a method: // For the 'GET /?a=1&b=2&op=+' method exposed by the API, you can invoke it by calling the following SDK method: Result output = client.rootGet("1", "2", "+"); // where the Result class of the SDK corresponds to the Result model of the API. // // For the 'GET /{a}/{b}/{op}' method exposed by the API, you can call the following SDK method to invoke the request, Result output = client.aBOpGet(a, b, c); // where a, b, c can be "1", "2", "add", respectively. // For the following API method: // POST / // host: ... // Content-Type: application/json // // { "a": 1, "b": 2, "op": "+" } // you can call invoke it by calling the rootPost method of the SDK as follows: Input body = new Input(); input.a=1; input.b=2; input.op="+"; Result output = client.rootPost(body); // where the Input class of the SDK corresponds to the Input model of the API. // Parse the result: // If the 'Result' object is { "a": 1, "b": 2, "op": "add", "c":3"}, you retrieve the result 'c') as String result=output.c;
  8. To use an Amazon Cognito credentials provider to authorize calls to your API, use the ApiClientFactory class to pass a set of AWS credentials by using the SDK generated by API Gateway, as shown in the following example.

    // Use CognitoCachingCredentialsProvider to provide AWS credentials // for the ApiClientFactory AWSCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider( context, // activity context "identityPoolId", // Cognito identity pool id Regions.US_EAST_1 // region of Cognito identity pool ); ApiClientFactory factory = new ApiClientFactory() .credentialsProvider(credentialsProvider);
  9. To set an API key by using the API Gateway- generated SDK, use code similar to the following.

    ApiClientFactory factory = new ApiClientFactory() .apiKey("YOUR_API_KEY");