Menggunakan Android SDK yang dihasilkan oleh API Gateway untuk REST API - Amazon API Gateway

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

Pada bagian ini, kami akan menguraikan langkah-langkah untuk menggunakan Android SDK yang dihasilkan oleh API Gateway untuk REST API. Sebelum melanjutkan lebih jauh, Anda harus sudah menyelesaikan langkah-langkah diMenghasilkan SDK untuk API menggunakan konsol API Gateway.

catatan

SDK yang dihasilkan tidak kompatibel dengan Android 4.4 dan versi sebelumnya. Untuk informasi selengkapnya, lihat Catatan penting Amazon API Gateway.

Untuk menginstal dan menggunakan SDK Android yang dihasilkan oleh API Gateway
  1. Ekstrak isi file.zip yang dihasilkan API Gateway-yang Anda unduh sebelumnya.

  2. Unduh dan instalApache Maven(sebaiknya versi 3.x).

  3. Unduh dan instalJDK.

  4. MengaturJAVA_HOMEVariabel Lingkungan.

  5. Jalankanmvn installperintah untuk menginstal file artefak dikompilasi ke repositori Maven lokal Anda. Ini menciptakantargetfolder yang berisi pustaka SDK dikompilasi.

  6. Salin file SDK (nama yang berasal dariId ArtifactdanVersi ArtifactAnda tentukan saat menghasilkan SDK, misalnya,simple-calcsdk-1.0.0.jar) daritargetfolder, bersama dengan semua perpustakaan lain daritarget/libfolder, ke proyek Andalibfolder.

    Jika Anda menggunakan Android Studio, buatlibsfolder di bawah modul aplikasi klien Anda dan salin file.jar yang diperlukan ke dalam folder ini. Pastikan 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 duplikasi file.jar dideklarasikan.

  7. MenggunakanApiClientFactorykelas untuk menginisialisasi SDK API Gateway-dihasilkan. Misalnya:

    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. Untuk menggunakan penyedia kredensi Amazon Cognito untuk mengotorisasi panggilan ke API Anda, gunakanApiClientFactorykelas untuk meneruskan satu setAWSkredensi dengan menggunakan SDK yang dihasilkan oleh API Gateway, seperti yang ditunjukkan dalam 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);
  9. Untuk mengatur kunci API dengan menggunakan API Gateway- dihasilkan SDK, gunakan kode yang mirip dengan berikut ini.

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