Get Started with Android SDK

This guide will help you get started with Contentstack Android SDK to build apps powered by Contentstack.

Prerequisites

To get started with Android SDK, you will need the following:

SDK Installation and Setup

To integrate your Android project with Contentstack, install the following dependency:

  • Gradle

    implementation 'com.contentstack.sdk:android:3.6.0'
  • Maven

    <dependency>
      <groupid>com.contentstack.sdk</groupid>
      <artifactid>android</artifactid>
      <version>3.6.0</version>
    </dependency>

Initialize SDK

Contentstack offers two regions (US and European) as data centers to store customers' account details and data. Both regions are independent of each other and therefore have a dedicated set of instructions to use SDKs offered by Contentstack.

To use SDKs for the European region, you will have to make certain changes in the configuration of the SDK, as detailed below, and the rest of the instructions remain the same.

To initialize the SDK, specify application context, stack’s API Key, delivery token, and name of the environment where will publish your content, as shown in the snippet below:

Stack stack = Contentstack.stack(context, "siteApiKey", "deliveryToken", "environment_name");

For Setting the European Region:

Refer the below code if you want to use European region:

Config config = Config();
Config.region = ContentstackRegion.EU;
Stack stack = Contentstack.stack(context, "API_key", "delivery_token", "environment_id", config);

Once you have initialized the SDK, you can query entries to fetch the required content.

Cache Policies

The cache policies allow you to define the source from where the SDK will retrieve the content. Based on the selected policy, the SDK can get the data from cache, network, or both.

Let’s look at the various cache policies available for use:

POLICIESDESCRIPTION
NETWORK_ONLY (default)If you set NETWORK_ONLY as the cache policy, the SDK retrieves data through a network call, and saves the retrieved data in the cache. This is set as the default policy.
CACHE_ELSE_NETWORKIf you set CACHE_ELSE_NETWORK as the cache policy, the SDK gets data from the cache. However, if it fails to retrieve data from the cache, it makes a network call.
NETWORK_ELSE_CACHEIf you set NETWORK_ELSE_CACHE as the cache policy, the SDK gets data using a network call. However, if the call fails, it retrieves data from cache.
CACHE_ONLYIf you set CACHE_ONLY as the cache policy, the SDK gets data from the cache.
CACHE_THEN_NETWORKIf you set CACHE_THEN_NETWORK as the cache policy, the SDK gets data from cache, and then makes a network call. (A success callback will be invoked twice.)
IGNORE_CACHEIf you set IGNORE_CACHE as the cache policy, the SDK always retrieves data by making a network call, without maintaining any cache.

You can set a cache policy on an entry, an asset, and/or a query object.

Setting a cache policy on an entry

To set the cache policy to all the query objects of an entry, refer to the code below:

public void entry_NETWORK_ONLY() {
    final Entry entry = stack.contentType("user").entry("blt11c1ad111ff1ddc1");
    entry.setCachePolicy(CachePolicy.NETWORK_ONLY);
    entry.addParam("key", "some_value");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                //Success block
            } else {
                //Error block
            }
        }
    });
}

Setting a cache policy on an asset

To set the cache policy to all the query objects of an asset, refer to the code below:

public void assets_NETWORK_ONLY() {
    stack.assetLibrary().setCachePolicy(CachePolicy.NETWORK_ONLY);
    stack.assetLibrary().fetchAll(new FetchAssetsCallback() {
        @Override
        public void onCompletion(ResponseType responseType, List < Asset > assets, Error error) {
            if (error == null) {
                // Success block
            } else {
                // Failure block
            }
        }
    });
}

Setting a cache policy on a query object

To set/override a cache policy on a specific query object, refer to the code below:

public void query_NETWORK_ONLY() {
    Query query = stack.contentType("categories").query();
    query.where("uid", "blt11c1ad111ff1ddc1");
    query.setCachePolicy(CachePolicy.NETWORK_ONLY);
    query.find(new QueryResultsCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, QueryResult queryresult, Error error) {
            if (error == null) {
                // Success block
            } else {
                // Failure block
            }
        }
    });
}

Basic Queries

Get a Single Entry

To retrieve a single entry from a content type use the code snippet given below:

ContentType contentType = stack.contentType("content_type_uid");
Entry blogEntry = contentType.entry("entry_uid");
blogEntry.fetch(new EntryResultCallBack() {
    @Override
    public void onCompletion(ResponseType responseType, Error error) {
        if (error == null) {
            // Success block
        } else {
            // Error block  
        }
    }
});

Get Multiple Entries

To retrieve multiple entries of a particular content type, use the code snippet given below:

//stack is an instance of Stack class
Query blogQuery = stack.contentType("content_type_uid").query();
blogQuery.find(new QueryResultsCallBack() {
    @Override
    public void onCompletion(ResponseType responseType, QueryResult queryResult, Error error) {
        if(error == null){
            //Success block
        }else{
            //Error block
        }
    }
});

These were examples of some of the basic queries of the SDK. For advanced queries, refer to Contentstack Android SDK API reference.

More articles in "Use Android SDK"

On This Page

top-arrow