Utilizzo di un SDK Android generato da API Gateway per un'API REST - Amazon API Gateway

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Utilizzo di un SDK Android generato da API Gateway per un'API REST

In questa sezione verranno illustrate le fasi necessarie per utilizzare un SDK Android generato da API Gateway per un'API REST. Per poter procedere, è necessario avere già completato le fasi descritte in Genera SDK per le API REST in API Gateway.

Nota

L'SDK generato non è compatibile con Android 4.4 e versioni precedenti. Per ulteriori informazioni, consulta Note importanti Amazon API Gateway.

Per installare e utilizzare l'SDK Android generato da API Gateway
  1. Estrai il contenuto del file .zip generato da API Gateway scaricato precedentemente.

  2. Scaricare e installare Apache Maven (preferibilmente versione 3.x).

  3. Scaricare e installare JDK 8.

  4. Imposta la variabile di ambiente JAVA_HOME.

  5. Eseguire il comando mvn install per installare i file di artefatto compilati nel repository Maven locale. Viene creata una cartella target contenente la libreria SDK compilata.

  6. Copiare il file SDK (il cui nome viene derivato dai valori specificati per Artifact Id (Id artefatto) e Artifact Version (Versione artefatto) durante la generazione dell'SDK, ad esempio simple-calcsdk-1.0.0.jar) dalla cartella target, insieme a tutte le altre librerie della cartella target/lib, nella cartella lib del progetto.

    Se usi Android Studio, crea una cartella libs nel modulo della tua app client e copia il file .jar richiesto in questa cartella. Verifica che la sezione delle dipendenze nel file gradle del modulo contenga il codice seguente.

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

    Assicurati che non siano dichiarati file .jar duplicati.

  7. Usa la classe ApiClientFactory per inizializzare l'SDK generato da API Gateway. Per esempio:

    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. Per utilizzare un provider di credenziali Amazon Cognito per autorizzare le chiamate alla tua API, usa la ApiClientFactory classe per passare un set di AWS credenziali utilizzando l'SDK generato da API Gateway, come mostrato nell'esempio seguente.

    // 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. Per impostare una chiave API utilizzando l'SDK generato da API Gateway, usa un codice simile al seguente.

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