Getting Started with Contentful and Android
This guide will show you how to get started using our Android client library to consume content.
Contentful's Content Delivery API (CDA) is a read-only API for retrieving content from Contentful. All content, both JSON and binary, is fetched from the server closest to a user's location by using our global CDN.
We publish client libraries for various languages to make developing applications easier.
Requirements
This tutorial assumes that you understand the Contentful data model.
Authentication
For every request, clients need to provide an API key, which is created per space and used to delimit applications and content classes.
You can create an access token using the Contentful web app or the Content Management API.
Create a new Android project
Create a new project in Android Studio using the 'Blank Activity' template, and name it whatever you wish.
Dependencies and permissions
This guide uses RXAndroid in the examples, which allows you to fetch results without tying up the main Android thread.
To include the CDA client library, add the following lines to the build.gradle file:
dependencies {
// [...]
compile 'com.contentful.java:java-sdk:9.0.1'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
}
Add the internet permission to the AndroidManifest.xml file so your app can access the Contentful APIs:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.demospaceexplorer" >
<uses-permission android:name="android.permission.INTERNET" />
...
</manifest>
Creating a client
Add the following code to the onCreate
method to create a CDAClient
that communicates with the Contentful APIs:
Initialize the client
You need an API key and a space ID to initialize a client. You can use the API key and space ID pre-filled below from our example space or replace them with your own values.
CDAClient client = CDAClient.builder()
.setSpace("<space_id>")
.setToken("<access_token>")
.build();
Use the Gson library to make JSON responses easier to read.
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Fetching specific items
If you want to fetch a specific entry, use the id
of the entry inside a .one
method:
client.observe(CDAEntry.class)
.one("<entry_id>")
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Subscriber<CDAEntry>() {
CDAEntry result;
@Override public void onCompleted() {
Log.i("Contentful", gson.toJson(result));
}
@Override public void onError(Throwable error) {
Log.e("Contentful", "could not request entry", error);
}
@Override public void onNext(CDAEntry cdaEntry) {
result = cdaEntry;
}
});
I/Contentful: CDAEntry{id='<entry_id>'}
Fetching all data from a space
To fetch all entries, create a new observable that watches for changes, in this case, fetching all entries from the specified space with the all
method and content type with the where
method:
client.observe(CDAEntry.class)
.where("content_type", "<product_content_type_id>")
.all()
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Subscriber<CDAArray>() {
CDAArray result;
@Override
public void onCompleted() {
for (CDAResource resource : result.items()) {
CDAEntry entry = (CDAEntry) resource;
Log.i("Contentful", entry.getField("productName").toString());
}
}
@Override
public void onError(Throwable error) {
Log.e("Contentful", "could not request entry", error);
}
@Override
public void onNext(CDAArray cdaArray) {
result = cdaArray;
}
});
I/Contentful: Hudson Wall Cup
I/Contentful: SoSo Wall Clock
I/Contentful: Whisk Beater
I/Contentful: Playsam Streamliner Classic Car, Espresso
The onNext
method saves the array of entries and the onCompleted
method is called once all entries are fetched from the API. The onError
method allows you to handle any problems.