

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用由 API Gateway 為 REST API 產生的 Android 軟體開發套件
<a name="how-to-generate-sdk-android"></a>

在本節中，我們將概述使用 API Gateway 為 REST API 所產生之 Android 軟體開發套件的步驟。您必須已完成[在 API Gateway 中為 REST API 產生 SDK](how-to-generate-sdk.md)中的步驟，才能繼續往下進行。

**注意**  
 所產生的開發套件與 Android 4.4 (含) 以前的版本不相容。如需詳細資訊，請參閱 [Amazon API Gateway 重要說明](api-gateway-known-issues.md)。

**安裝及使用 API Gateway 所產生的 Android 軟體開發套件**

1. 將您稍早下載之 API Gateway 所產生的 .zip 檔案內容解壓縮。

1. 下載並安裝 [Apache Maven](https://maven.apache.org/) (最好是 3.x 版)。

1. 下載並安裝 [JDK 8](https://docs.oracle.com/javase/8/docs/technotes/guides/install/install_overview.html)。

1. 設定 `JAVA_HOME` 環境變數。

1. 執行 **mvn install** 命令，將已編譯的成品檔案安裝到您的本機 Maven 儲存庫。這會建立 `target` 資料夾，其中包含已編譯的開發套件程式庫。

1. 將 `target` 資料夾中的開發套件檔案 (其名稱衍生自您在產生開發套件時所指定的 **Artifact Id** (成品 ID) 與 **Artifact Version** (成品版本)，例如 `simple-calcsdk-1.0.0.jar`)，連同 `target/lib` 資料夾中的所有其他程式庫，一起複製到您專案的 `lib` 資料夾中。

   如果您使用 Android Studio，請在您的用戶端應用程式模組下建立一個 `libs` 資料夾，然後將必要的 .jar 檔案複製到此資料夾中。確認模組之 Gradle 檔案中的相依性區段包含以下內容。

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

   確定未宣告重複的 .jar 檔案。

1. 使用 `ApiClientFactory` 類別來初始化 API Gateway 產生的軟體開發套件。例如：

   ```
   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. 若要使用 Amazon Cognito 憑證提供者授權呼叫您的 API，請使用 `ApiClientFactory` 類別透過 API Gateway 所產生的軟體開發套件來傳遞一組 AWS 憑證，如下列範例所示。

   ```
   // 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. 若要使用 API Gateway 所產生的軟體開發套件來設定 API 金鑰，請使用類似如下的程式碼。

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