

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

# Menggunakan Android SDK yang dihasilkan oleh API Gateway untuk REST API
<a name="how-to-generate-sdk-android"></a>

Di bagian ini, kami akan menguraikan langkah-langkah untuk menggunakan SDK Android yang dihasilkan oleh API Gateway untuk REST API. Sebelum melangkah lebih jauh, Anda harus sudah menyelesaikan langkah-langkahnya. [Hasilkan SDKs untuk REST APIs di API Gateway](how-to-generate-sdk.md)

**catatan**  
 SDK yang dihasilkan tidak kompatibel dengan Android 4.4 dan yang lebih lama. Untuk informasi selengkapnya, lihat [Catatan penting Amazon API Gateway](api-gateway-known-issues.md). 

**Untuk menginstal dan menggunakan Android SDK yang dihasilkan oleh API Gateway**

1. Ekstrak konten file.zip yang dihasilkan API Gateway yang Anda unduh sebelumnya.

1. Unduh dan instal [Apache Maven](https://maven.apache.org/) (lebih disukai versi 3.x).

1. Unduh dan instal [JDK 8](https://docs.oracle.com/javase/8/docs/technotes/guides/install/install_overview.html).

1. Mengatur variabel `JAVA_HOME` lingkungan.

1. Jalankan **mvn install** perintah untuk menginstal file artefak yang dikompilasi ke repositori Maven lokal Anda. Ini membuat `target` folder yang berisi pustaka SDK yang dikompilasi.

1. Salin file SDK (nama yang berasal dari Id **Artifact** dan Versi **Artifact** yang Anda tentukan saat membuat SDK, misalnya,) dari `target` folder`simple-calcsdk-1.0.0.jar`, bersama dengan semua pustaka lain dari `target/lib` folder, ke folder proyek Anda. `lib`

   Jika Anda menggunakan Android Studio, buat `libs` folder di bawah modul aplikasi klien Anda dan salin file.jar yang diperlukan ke folder ini. Verifikasi bahwa bagian dependensi dalam file gradle modul berisi yang berikut ini.

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

   Pastikan tidak ada file.jar duplikat yang dideklarasikan.

1. Gunakan `ApiClientFactory` class untuk menginisialisasi SDK yang dihasilkan API GateWay-generated. Sebagai contoh:

   ```
   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;
   ```

1. Untuk menggunakan penyedia kredensial Amazon Cognito untuk mengotorisasi panggilan ke API Anda, gunakan `ApiClientFactory` kelas untuk meneruskan sekumpulan AWS kredensional menggunakan SDK yang dihasilkan oleh API Gateway, seperti yang ditunjukkan pada contoh berikut.

   ```
   // 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);
   ```

1. Untuk menyetel kunci API menggunakan SDK yang dihasilkan API Gateway, gunakan kode yang mirip dengan berikut ini.

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