Page updated Jan 16, 2024

Fetch data

Amplify Android v1 is now in Maintenance Mode until May 31st, 2024. This means that we will continue to include updates to ensure compatibility with backend services and security. No new features will be introduced in v1.

Please use the latest version (v2) of Amplify Library for Android to get started.

If you are currently using v1, follow these instructions to upgrade to v2.

Amplify libraries should be used for all new cloud connected applications. If you are currently using the AWS Mobile SDK for Android, you can access the documentation here.

GET requests

To make a GET request, first build a RestOptions object and then use the Amplify.API.get api to issue the request:

1void getTodo() {
2 RestOptions options = RestOptions.builder()
3 .addPath("/todo")
4 .build();
5
6 Amplify.API.get(options,
7 restResponse -> Log.i("MyAmplifyApp", "GET succeeded: " + restResponse),
8 apiFailure -> Log.e("MyAmplifyApp", "GET failed.", apiFailure)
9 );
10}
1private fun getTodo() {
2 val request = RestOptions.builder()
3 .addPath("/todo")
4 .build()
5
6 Amplify.API.get(request,
7 { Log.i("MyAmplifyApp", "GET succeeded: $it") },
8 { Log.e("MyAmplifyApp", "GET failed.", it) }
9 )
10}
1private suspend fun getTodo() {
2 val request = RestOptions.builder()
3 .addPath("/todo")
4 .build()
5
6 try {
7 val response = Amplify.API.get(request)
8 Log.i("MyAmplifyApp", "GET succeeded: ${response.data}")
9 } catch (error: ApiException) {
10 Log.e("MyAmplifyApp", "GET failed", error)
11 }
12}
1void getTodo() {
2 RestOptions options = RestOptions.builder()
3 .addPath("/todo")
4 .build();
5
6 RxAmplify.API.get(options)
7 .subscribe(
8 restResponse -> Log.i("MyAmplifyApp", "GET succeeded: " + restResponse),
9 apiFailure -> Log.e("MyAmplifyApp", "GET failed.", apiFailure)
10 );
11}

Accessing query parameters & body in Lambda proxy function

To learn more about Lambda Proxy Integration, please visit Amazon API Gateway Developer Guide.

If you are using a REST API which is generated with Amplify CLI, your backend is created with Lambda Proxy Integration, and you can access your query parameters & body within your Lambda function via the event object:

1exports.handler = function(event, context, callback) {
2 console.log(event.queryStringParameters);
3 console.log('body: ', event.body);
4}

Alternatively, you can update your backend file which is located at amplify/backend/function/[your-lambda-function]/app.js with the middleware:

1const awsServerlessExpressMiddleware = require('aws-serverless-express/middleware');
2app.use(awsServerlessExpressMiddleware.eventContext());

Accessing Query Parameters with Serverless Express

In your request handler use req.apiGateway.event or req.query:

1app.get('/todo', function(req, res) {
2 const query = req.query;
3 // or
4 // const query = req.apiGateway.event.queryStringParameters
5 res.json({
6 event: req.apiGateway.event, // to view all event data
7 query: query
8 });
9});

Then you can use query parameters in your path as follows:

1RestOptions options = RestOptions.builder()
2 .addPath("/todo")
3 .addQueryParameters(Collections.singletonMap("q", "test"))
4 .build();
5
6Amplify.API.get(options,
7 response -> Log.i("MyAmplifyApp", "GET succeeded: " + response),
8 error -> Log.e("MyAmplifyApp", "GET failed.", error)
9);
1val request = RestOptions.builder()
2 .addPath("/todo")
3 .addQueryParameters(mapOf("q" to "test"))
4 .build()
5
6Amplify.API.get(request,
7 { Log.i("MyAmplifyApp", "GET succeeded: $it") },
8 { Log.e("MyAmplifyApp", "GET failed", it) }
9)
1val request = RestOptions.builder()
2 .addPath("/todo")
3 .addQueryParameters(mapOf("q" to "test"))
4 .build()
5
6try {
7 val response = Amplify.API.get(request)
8 Log.i("MyAmplifyApp", "GET succeeded: $response.")
9} catch (error: ApiException) {
10 Log.e("MyAmplifyApp", "GET failed", error)
11}
1RestOptions options = RestOptions.builder()
2 .addPath("/todo")
3 .addQueryParameters(Collections.singletonMap("q", "test"))
4 .build();
5
6RxAmplify.API.get(options)
7 .subscribe(
8 response -> Log.i("MyAmplifyApp", "GET succeeded: " + response),
9 error -> Log.e("MyAmplifyApp", "GET failed.", error)
10 );