Getting started with Contentful and Android

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 an user's location by using our global CDN.

We publish SDKs for various languages to make developing applications easier. This article details how to get content using the JavaScript CDA SDK.

Pre-requisites

This tutorial assumes you have read and understood the guide that covers the Contentful data model.

We assume you have Android Studio installed, and are familiar with it.

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.

Define Contentful as a dependency

To include the CDA SDK in your app, add the following lines to the build.gradle file:

1
2
3
4
dependencies {
    // [...]
    compile 'com.contentful.java:java-sdk:7.3.0'
}

Fetching all data from a demo space

Add the internet permission to the AndroidManifest.xml file so your app can access the Contentful APIs:

1
2
3
4
5
6
7
8
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.demospaceexplorer" >

    <uses-permission android:name="android.permission.INTERNET" />

<!-- ... -->

</manifest>

Add the following code to the click handler of the floating action button, or in the onCreate function:

1
2
3
4
CDAClient client = CDAClient.builder()
        .setSpace("cfexampleapi")
        .setToken("b4c0n73n7fu1")
        .build();

This will create a CDAClient that communicates with the Contentful APIs using the space id and token you gained earlier.

To fetch all entries:

1
2
3
4
5
6
client.fetch(CDAEntry.class).all(new CDACallback<CDAArray>() {
    @Override
    protected void onSuccess(CDAArray result) {
        // do something with the result.
    }
});

The onSuccess handler is called once the data is loaded and you can process the results. In production applications you should override the failure handler to cope with any problems.

1
2
protected void onFailure(Throwable error) {
}

Fetching specific items

If you want to fetch the data of a specific entry, use the id of the entry:

1
2
3
4
5
6
client.fetch(CDAEntry.class).one("happycat", new CDACallback<CDAEntry>() {
    @Override
    protected void onSuccess(CDAEntry result) {
        textView.setText(result.toString());
    }
});