Name:
interface
Value:
Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Page updated Apr 29, 2024

Read application data

Amplify Android v1 is deprecated as of June 1st, 2024. No new features or bug fixes will be added. Dependencies may become outdated and potentially introduce compatibility issues.

Please use the latest version (v2) of Amplify Library for Android to get started. Refer to the upgrade guide for instructions on upgrading your application to the latest version.

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.

Query item

Now that you were able to make a mutation, take the Id that was printed out and use it in your query to retrieve data.

private void getTodo(String id) {
Amplify.API.query(
ModelQuery.get(Todo.class, id),
response -> Log.i("MyAmplifyApp", ((Todo) response.getData()).getName()),
error -> Log.e("MyAmplifyApp", error.toString(), error)
);
}
private fun getTodo(id: String) {
Amplify.API.query(ModelQuery.get(Todo::class.java, id),
{ Log.i("MyAmplifyApp", "Query results = ${(it.data as Todo).name}") },
{ Log.e("MyAmplifyApp", "Query failed", it) }
);
}
suspend fun getTodo(id: String) {
try {
val response = Amplify.API.query(ModelQuery.get(Todo::class.java, id))
Log.i("MyAmplifyApp", response.data.name)
} catch (error: ApiException) {
Log.e("MyAmplifyApp", "Query failed", error)
}
}
private void getTodo(String id) {
RxAmplify.API.query(ModelQuery.get(Todo.class, id))
.subscribe(
response -> Log.i("MyAmplifyApp", ((Todo) response.getData()).getName()),
error -> Log.e("MyAmplifyApp", error.toString(), error)
);
}

List items

You can get the list of items that match a condition that you specify in Amplify.API.query:

Amplify.API.query(
ModelQuery.list(Todo.class, Todo.NAME.contains("first")),
response -> {
for (Todo todo : response.getData()) {
Log.i("MyAmplifyApp", todo.getName());
}
},
error -> Log.e("MyAmplifyApp", "Query failure", error)
);
Amplify.API.query(
ModelQuery.list(Todo::class.java, Todo.NAME.contains("first")),
{ response ->
response.data.forEach { todo ->
Log.i("MyAmplifyApp", todo.name)
}
},
{ Log.e("MyAmplifyApp", "Query failure", it) }
)
try {
Amplify.API
.query(ModelQuery.list(Todo::class.java, Todo.NAME.contains("first")))
.response.data
.items.forEach { todo -> Log.i("MyAmplifyApp", todo.name) }
} catch (error: ApiException) {
Log.e("MyAmplifyApp", "Query failure", error)
}
RxAmplify.API.query(ModelQuery.list(Todo.class, Todo.NAME.contains("first"))
.subscribe(
response -> {
for (Todo todo : response.getData()) {
Log.i("MyAmplifyApp", todo.getName());
}
},
error -> Log.e("MyAmplifyApp", "Query failure", error)
);

Note: This approach will only return up to the first 1,000 items. To change this limit or make requests for additional results beyond this limit, use pagination as discussed below.

List subsequent pages of items

A list query only returns the first 1,000 items by default, so for large data sets, you'll need to paginate through the results. After receiving a page of results, you can obtain a GraphQLRequest for requesting the next page, if there are more results available. The page size is configurable as well, as in the example below.

public void queryFirstPage() {
query(ModelQuery.list(Todo.class, ModelPagination.limit(1_000)));
}
private static void query(GraphQLRequest<PaginatedResult<Todo>> request) {
Amplify.API.query(
request,
response -> {
if (response.hasData()) {
for (Todo todo : response.getData()) {
Log.d("MyAmplifyApp", todo.getName());
}
if (response.getData().hasNextResult()) {
query(response.getData().getRequestForNextResult());
}
}
},
failure -> Log.e("MyAmplifyApp", "Query failed.", failure)
);
}
fun queryFirstPage() {
query(ModelQuery.list(Todo::class.java, ModelPagination.limit(1_000)))
}
fun query(request: GraphQLRequest<PaginatedResult<Todo>>) {
Amplify.API.query(request,
{ response ->
if (response.hasData()) {
response.data.items.forEach { todo ->
Log.d("MyAmplifyApp", todo.name)
}
if (response.data.hasNextResult()) {
query(response.data.requestForNextResult)
}
}
},
{ Log.e("MyAmplifyApp", "Query failed", it) }
)
}
suspend fun queryFirstPage() {
query(ModelQuery.list(Todo::class.java,
ModelPagination.firstPage().withLimit(1_000)))
}
suspend fun query(request: GraphQLRequest<PaginatedResult<Todo>>) {
try {
val response = Amplify.API.query(request)
response.data.items.forEach { todo ->
Log.d("MyAmplifyApp", todo.name)
}
if (response.data.hasNextResult()) {
query(response.data.requestForNextResult)
}
} catch (error: ApiException) {
Log.e("MyAmplifyApp", "Query failed.", error)
}
}
BehaviorSubject<GraphQLRequest<PaginatedResult<Todo>>> subject =
BehaviorSubject.createDefault(ModelQuery.list(Todo.class, ModelPagination.limit(1_000)));
subject.concatMap(request -> RxAmplify.API.query(request).toObservable())
.doOnNext(response -> {
if (response.hasErrors()) {
subject.onError(new Exception(String.format("Query failed: %s", response.getErrors())));
} else if (!response.hasData()) {
subject.onError(new Exception("Empty response from AppSync."));
} else if(response.getData().hasNextResult()) {
subject.onNext(response.getData().getRequestForNextResult());
} else {
subject.onComplete();
}
})
.concatMapIterable(GraphQLResponse::getData)
.subscribe(
todo -> Log.d(TAG, "Todo: " + todo),
error -> Log.e(TAG, "Error: " + error)
);