Entry

View as Markdown

Entry

An entry is one piece of content created from a content type. Use this class to

  • fetch one published entry by its UID
  • choose the locale, variant, fields and references that the response carries
  • read the field values of the fetched entry, including assets, Group fields and referenced entries

stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>") returns an Entry. The builder methods record request options on it, fetch sends one request, and the getters read the stored response.

addParam

addParam adds one query parameter to the request that fetch sends.

NameTypeDescription
key (required)String

Name of the query parameter.

Default: Not applicable
value (required)String

Value of the query parameter.

Default: Not applicable

Returns: Entry. The same entry, for chaining.

Validation

  • There is no client-side validation. When key or value is null, addParam records nothing.
  • An invalid value surfaces as an Error in the callback when fetch sends the request.

Additional Resource Refer to Content Delivery API Errors for the full error code list.

Behavior

  • Client-side only. addParam records the pair, and fetch sends it as key=value.
  • The SDK does not URL-encode the key or the value. Encode a value that holds characters such as &, =, # or a space before you pass it, for example with java.net.URLEncoder.
  • addParam shares one parameter store with setLocale, includeFallback and the other builder methods. A second value for the same key replaces the first, whichever method recorded it.
  • Use it for API parameters that have no dedicated method, for example include_all, include_all_depth or include_taxonomy_terms.
  • The value stays on the entry for every later fetch.

Example

Basic usage: resolve taxonomy terms in the response

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>")
            .addParam("include_taxonomy_terms", "true");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                Log.d("Contentstack", "Taxonomies: " + entry.getJSONArray("taxonomies"));
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

Include every reference field up to three levels deep

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>")
            .addParam("include_all", "true")
            .addParam("include_all_depth", "3");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error != null) {
                Log.e("Contentstack", error.getErrorMessage());
                return;
            }
            Log.d("Contentstack", "Entry with references: " + entry.toJSON());
        }
    });
} catch (Exception e) {
    Log.e("Contentstack", e.getMessage());
}

cancelRequest

cancelRequest cancels every entry request that the app has in progress.

Returns: void. This method returns nothing.

Validation

  • There is no client-side validation. cancelRequest cannot fail.

Behavior

  • cancelRequest cancels the in-progress fetch requests of every Entry object in the app, not only the entry you call it on.
  • Volley does not deliver a cancelled request, so onCompletion does not fire for it.
  • cancelRequest does not affect query requests, cache reads, or requests that already finished.
  • A fetch that you call afterwards runs normally.

Limitations

  • Does not cancel the request of one entry only. Ignore the result in your callback instead, for example by checking a flag that you set when the screen closes.

Example

Basic usage: cancel the request when the screen closes

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                Log.d("Contentstack", "Title: " + entry.getTitle());
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
    // Cancel, for example from onStop, when the result is no longer needed.
    // The callback above does not fire for a cancelled request.
    entry.cancelRequest();
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

configure

configure loads entry data from a JSON object that you already hold, without a request.

NameTypeDescription
jsonObject (required)JSONObject

Entry JSON to load into this entry.

Default: Not applicable

Returns: Entry. The same entry, for chaining.

Validation

  • There is no client-side validation. configure does not throw.
  • A null argument clears the stored data and the UID of the entry.

Behavior

  • Pass the entry object itself, for example the value of the entry key of a response, not the whole response body.
  • configure reads the same values that fetch reads: the UID, title, URL, locale, tags, _owner and _metadata. The getters work on the result.
  • The UID comes from the uid key. An object without a uid key sets the UID to a single space.
  • configure replaces the data the entry held. It keeps the recorded query parameters and headers.

Example

Basic usage: load an entry the app stored earlier

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.Stack;

import org.json.JSONObject;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    // Entry JSON that the app saved from an earlier response
    JSONObject json = new JSONObject();
    json.put("uid", "<ENTRY_UID>");
    json.put("title", "Spring launch");
    json.put("locale", "en-us");
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>").configure(json);
    Log.d("Contentstack", "Title: " + entry.getTitle() + ", locale: " + entry.getLocale());
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null. JSONObject.put throws JSONException.
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

contains

contains checks whether the entry holds a field.

NameTypeDescription
key (required)String

UID of the field to read.

Default: Not applicable

Returns: Boolean. Whether the entry holds the field.

Validation

  • There is no client-side validation. contains does not throw.
  • A null key returns false.

Behavior

  • contains returns true for a field whose value is JSON null.
  • It checks the top-level keys of the entry only. For a field inside a Group field, read the group first with getGroup.
  • Call it after fetch succeeds, or after configure. Before that, the entry holds no data and contains returns false.

Example

Basic usage: read a value with contains

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                if (entry.contains("subtitle")) {
                    Log.d("Contentstack", "Subtitle: " + entry.getString("subtitle"));
                }
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

except

except removes the top-level fields you name from the response.

NameTypeDescription
fieldUid (required)String[]

UIDs of the fields to leave out.

Default: Not applicable

Returns: Entry. The same entry, for chaining.

Validation

  • There is no client-side validation. except ignores a null or empty array.
  • An invalid value surfaces as an Error in the callback when fetch sends the request.

Additional Resource Refer to Content Delivery API Errors for the full error code list.

Behavior

  • Client-side only. except records the field UIDs and makes no request. fetch sends each one as an except[BASE][] query parameter, as the Exclude operator describes.
  • A second call adds its UIDs to the list.
  • For a field inside a Group field, pass the path with a dot, for example group_uid.field_uid. For a field inside a block, pass modular_block_uid.block_uid.field_uid.
  • The list applies to the next fetch only. fetch clears it when it sends a network request, so call except again before each fetch.
  • To apply the same filter to the fields of a referenced entry, use the exceptWithReferenceUid method.

Example

Basic usage: leave out some fields

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>")
            .except(new String[]{"body", "seo"});
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                Log.d("Contentstack", "Fields: " + entry.toJSON().names());
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

Fields inside a Group field

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>")
            .except(new String[]{"seo.meta_description"});
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error != null) {
                Log.e("Contentstack", error.getErrorMessage());
                return;
            }
            Log.d("Contentstack", "SEO group: " + entry.getJSONObject("seo"));
        }
    });
} catch (Exception e) {
    Log.e("Contentstack", e.getMessage());
}

exceptWithReferenceUid

exceptWithReferenceUid includes a referenced entry and removes the fields you name from it.

NameTypeDescription
fieldUid (required)ArrayList<String>

Referenced-entry fields to leave out.

Default: Not applicable
referenceFieldUid (required)String

UID of the reference field.

Default: Not applicable

Returns: Entry. The same entry, for chaining.

Validation

  • There is no client-side validation. When fieldUid or referenceFieldUid is null, exceptWithReferenceUid records nothing.
  • An invalid value surfaces as an Error in the callback when fetch sends the request.

Additional Resource Refer to Content Delivery API Errors for the full error code list.

Behavior

  • Client-side only. exceptWithReferenceUid records the field UIDs, and fetch sends each one as an except[<referenceFieldUid>][] query parameter, as the Exclude operator describes.
  • exceptWithReferenceUid also calls includeReference with referenceFieldUid, so you do not need a separate includeReference call for that field.
  • A second call for the same reference field replaces its list. Calls for different reference fields add up.
  • An empty fieldUid list includes the referenced entry and sends no except parameter for it.
  • The field list applies to the next fetch only, because fetch clears it when it sends a network request. The include[] value stays for later calls.
  • To filter the top-level fields of the entry itself, use the except method.

Example

Basic usage: filter the fields of a referenced entry

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import java.util.ArrayList;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    ArrayList<String> authorFields = new ArrayList<>();
    authorFields.add("bio");
    authorFields.add("social_links");
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>")
            .exceptWithReferenceUid(authorFields, "authors");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                Log.d("Contentstack", "Authors: " + entry.getJSONArray("authors"));
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

Filter the entry and its reference together

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import java.util.ArrayList;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    ArrayList<String> authorFields = new ArrayList<>();
    authorFields.add("title");
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>")
            .except(new String[]{"body"})
            .exceptWithReferenceUid(authorFields, "authors");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error != null) {
                Log.e("Contentstack", error.getErrorMessage());
                return;
            }
            Log.d("Contentstack", "Title: " + entry.getTitle() + ", authors: " + entry.getJSONArray("authors"));
        }
    });
} catch (Exception e) {
    Log.e("Contentstack", e.getMessage());
}

fetch

fetch retrieves one published entry by its UID and stores the response on the entry object.

NameTypeDescription
callBack (required)EntryResultCallBack

Receives the outcome when the request ends.

Default: Not applicable

Returns: void. The result arrives in the callback.

Validation

  • fetch checks that the entry has a UID. When the UID is null or empty, it calls onCompletion with an Error and ResponseType.UNKNOWN and sends no request, because the request path needs the entry UID.
  • fetch does not throw for an API rejection or a network failure.
    • Both arrive as a non-null error in onCompletion, and the entry keeps the values it held before the call.
    • Check error in the callback even when the call sits inside a try block.
  • An exception while fetch builds the request, for example after setCachePolicy received null, also arrives as an Error with ResponseType.UNKNOWN.

Additional Resource Refer to Content Delivery API Errors for the full error code list.

Behavior

  • One call sends one Get a single entry request (GET /v3/content_types/{content_type_uid}/entries/{entry_uid}). It carries the stack headers, the headers of this entry, and every query parameter that the builder methods recorded.
  • fetch also copies the environment header into the environment query parameter.
  • onCompletion(ResponseType responseType, Error error) fires on the main thread when the request ends.
    • On success error is null and the entry holds the fetched fields. Read them with the getters, for example getTitle and getString.
    • responseType is NETWORK for a network response, CACHE for a cached copy, and UNKNOWN for an error raised before any request.
  • The cache policy decides whether fetch reads the network, the disk cache, or both. The default is NETWORK_ONLY. With CACHE_THEN_NETWORK, the callback can fire twice. See the setCachePolicy method.
  • When it builds a network request, fetch clears the field lists that only, except, onlyWithReferenceUid and exceptWithReferenceUid recorded. Every other recorded value stays for the next call.
  • fetch stores only the entry object of the response body. The entry object exposes no other key of the body.
  • The API returns only the latest published version of the entry, as Get a single entry states.

Example

Basic usage: fetch an entry by UID

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                Log.d("Contentstack", "Title: " + entry.getTitle());
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

All builders: shape the response before the request

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>")
            .setLocale("<LOCALE>")
            .includeFallback()
            .includeReference("authors")
            .includeEmbeddedItems()
            .only(new String[]{"title", "url", "body", "authors"});
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error != null) {
                Log.e("Contentstack", error.getErrorMessage());
                return;
            }
            Log.d("Contentstack", "Locale: " + entry.getLocale() + ", title: " + entry.getTitle());
        }
    });
} catch (Exception e) {
    Log.e("Contentstack", e.getMessage());
}

Edge case: an entry without a UID

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    // An empty UID makes fetch report an error without sending a request
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error != null) {
                Log.e("Contentstack", error.getErrorMessage());
                return;
            }
            // Not reached: fetch reports the missing UID through error
            Log.d("Contentstack", "Title: " + entry.getTitle());
        }
    });
} catch (Exception e) {
    Log.e("Contentstack", e.getMessage());
}

get

get returns the value of a field as an Object.

NameTypeDescription
key (required)String

UID of the field to read.

Default: Not applicable

Returns: Object. The raw field value, or null.

Validation

  • There is no client-side validation. get does not throw.
  • A null key returns null.

Behavior

  • get returns the value in the type that the JSON parser stores. For example, a text field returns a String, a number field an Integer or a Double, and a Group field a JSONObject.
  • A field whose value is JSON null returns JSONObject.NULL, not null.
  • get returns null when the field is missing.
  • Use a typed getter, for example getString or getInt, to avoid a cast.
  • Call it after fetch succeeds, or after configure. Before that, the entry holds no data and get returns null.

Example

Basic usage: read a value with get

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                Object value = entry.get("price");
                if (value instanceof Number) {
                    Log.d("Contentstack", "Price: " + value);
                }
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getAllEntries

getAllEntries returns the entries in a reference field as Entry objects.

NameTypeDescription
refKey (required)String

UID of the reference field.

Default: Not applicable
refContentType (required)String

Content type UID to give each entry.

Default: Not applicable

Returns: ArrayList<Entry>. The referenced entries as Entry objects, or null.

Validation

  • There is no client-side validation. getAllEntries does not throw.
  • It returns null when refKey names a missing field or a field that does not hold a JSON array, and when the entry holds no data yet.

Behavior

  • getAllEntries builds one Entry per item of the reference field, and sends no request.
  • Each returned entry holds the UID, tags, owner and JSON of its item. It holds no stored title or URL, so read them with getString("title") and getString("url"), not getTitle or getURL.
  • Without includeReference on the parent request, each item holds only the UID of the referenced entry. Each returned entry can call fetch to load its full content.
  • getAllEntries does not check refContentType against the data. getContentType on each returned entry returns it.

Example

Basic usage: read the entries of a reference field

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import java.util.ArrayList;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>")
            .includeReference("authors");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                ArrayList<Entry> authors = entry.getAllEntries("authors", "author");
                if (authors != null) {
                    for (Entry author : authors) {
                        Log.d("Contentstack", author.getUid() + ": " + author.getString("title"));
                    }
                }
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getAsset

getAsset returns the asset in a file field as an Asset object.

NameTypeDescription
key (required)String

UID of the file field.

Default: Not applicable

Returns: Asset. The asset in the field.

Validation

  • There is no client-side validation.
  • getAsset throws NullPointerException when the field is missing or does not hold one JSON object, and when the entry holds no data yet.

Behavior

  • getAsset builds the Asset from the JSON that the entry already holds, and sends no request. Read it with the Asset getters, for example getUrl and getFileName.
  • For a file field that holds several assets, use the getAssets method.
  • Known issue: A missing or empty file field makes getAsset crash the app with NullPointerException. Call it only after getJSONObject(key) returns a non-null value, as the example below shows.

Example

Basic usage: read a value with getAsset

import android.util.Log;

import com.contentstack.sdk.Asset;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                if (entry.getJSONObject("banner") != null) {
                    Asset banner = entry.getAsset("banner");
                    Log.d("Contentstack", "Banner URL: " + banner.getUrl());
                }
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getAssets

getAssets returns the assets in a multiple file field as a list of Asset objects.

NameTypeDescription
key (required)String

UID of the multiple file field.

Default: Not applicable

Returns: List<Asset>. The assets in the field.

Validation

  • There is no client-side validation.
  • getAssets throws NullPointerException when the field is missing or does not hold a JSON array, and when the entry holds no data yet.

Behavior

  • getAssets builds one Asset per JSON object in the field, and sends no request. It skips an item that is not a JSON object.
  • For a file field that holds one asset, use the getAsset method.
  • Known issue: A missing or empty file field makes getAssets crash the app with NullPointerException. Call it only after getJSONArray(key) returns a non-null value, as the example below shows.

Example

Basic usage: read a value with getAssets

import android.util.Log;

import com.contentstack.sdk.Asset;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import java.util.List;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                if (entry.getJSONArray("gallery") != null) {
                    List<Asset> gallery = entry.getAssets("gallery");
                    for (Asset image : gallery) {
                        Log.d("Contentstack", "Image: " + image.getFileName());
                    }
                }
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getBoolean

getBoolean returns the value of a field as a Boolean.

NameTypeDescription
key (required)String

UID of the field to read.

Default: Not applicable

Returns: Boolean. The field value as a Boolean, or false.

Validation

  • There is no client-side validation. getBoolean does not throw.
  • A null key returns false.

Behavior

  • getBoolean returns false when the field is missing or its value is not a JSON Boolean. The string "true" also returns false.
  • Use contains to tell a stored false from a missing field.
  • Call it after fetch succeeds, or after configure. Before that, the entry holds no data and getBoolean returns false.

Example

Basic usage: read a value with getBoolean

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                Boolean featured = entry.getBoolean("is_featured");
                Log.d("Contentstack", "Featured: " + featured);
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getContentType

getContentType returns the UID of the content type of the entry.

Returns: String. The UID of the entry's content type.

Validation

  • There is no client-side validation. getContentType does not throw.

Behavior

  • getContentType returns the UID you passed to stack.contentType("<CONTENT_TYPE_UID>"). It does not read the response, so it returns the value before any request too.
  • For an entry from the getAllEntries method, it returns the refContentType argument of that call.

Example

Basic usage: read a value with getContentType

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                Log.d("Contentstack", "Content type: " + entry.getContentType());
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getCreateAt

getCreateAt returns the creation time of the entry as a Calendar.

Returns: Calendar. The entry's creation time, or null.

Validation

  • There is no client-side validation. getCreateAt does not throw.

Behavior

  • getCreateAt parses the created_at value of the entry, the same way getDate does. It accepts ISO 8601 date-time values and date-only values such as 2024-05-10.
  • It returns null when the entry has no created_at key, or when the value matches no accepted format.
  • Call it after fetch succeeds, or after configure. Before that, the entry holds no data and getCreateAt returns null.
  • Known issue: getCreateAt reads a UTC date-time value, such as 2024-05-10T10:24:13.000Z, as device-local time, so the returned Calendar is off by the UTC offset of the device. Read the value with getString and parse it with a formatter set to the UTC time zone instead.

Example

Basic usage: read a value with getCreateAt

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import java.util.Calendar;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                Calendar value = entry.getCreateAt();
                Log.d("Contentstack", "created_at: " + (value != null ? value.getTime() : "not set"));
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getCreatedBy

getCreatedBy returns the UID of the user who created the entry.

Returns: String. The UID of the user who created the entry.

Validation

  • There is no client-side validation. getCreatedBy does not throw.

Behavior

  • getCreatedBy reads the created_by value of the entry with getString.
  • It returns null when the entry has no created_by key.
  • Call it after fetch succeeds, or after configure. Before that, the entry holds no data and getCreatedBy returns null.

Example

Basic usage: read a value with getCreatedBy

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                Log.d("Contentstack", "created_by: " + entry.getCreatedBy());
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getDate

getDate returns the value of a date field as a Calendar.

NameTypeDescription
key (required)String

UID of the field to read.

Default: Not applicable

Returns: Calendar. The field value as a Calendar, or null.

Validation

  • There is no client-side validation. getDate does not throw.
  • A null key returns null.

Behavior

  • getDate parses the string value of the field. It accepts ISO 8601 date-time values and date-only values such as 2024-05-10.
  • It returns null when the field is missing, is not a string, or matches no accepted format.
  • Call it after fetch succeeds, or after configure. Before that, the entry holds no data and getDate returns null.
  • Known issue: getDate reads a UTC date-time value, such as 2024-05-10T10:24:13.000Z, as device-local time, so the returned Calendar is off by the UTC offset of the device. Read the value with getString and parse it with a formatter set to the UTC time zone instead.

Example

Basic usage: read a value with getDate

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import java.util.Calendar;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                Calendar launch = entry.getDate("launch_date");
                if (launch != null) {
                    Log.d("Contentstack", "Launch year: " + launch.get(Calendar.YEAR));
                }
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getDeleteAt

getDeleteAt returns the deletion time of the entry as a Calendar.

Returns: Calendar. The entry's deletion time, or null.

Validation

  • There is no client-side validation. getDeleteAt does not throw.

Behavior

  • getDeleteAt parses the deleted_at value of the entry, the same way getDate does. It accepts ISO 8601 date-time values and date-only values such as 2024-05-10.
  • It returns null when the entry has no deleted_at key, or when the value matches no accepted format.
  • Call it after fetch succeeds, or after configure. Before that, the entry holds no data and getDeleteAt returns null.
  • Known issue: getDeleteAt reads a UTC date-time value, such as 2024-05-10T10:24:13.000Z, as device-local time, so the returned Calendar is off by the UTC offset of the device. Read the value with getString and parse it with a formatter set to the UTC time zone instead.

Example

Basic usage: read a value with getDeleteAt

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import java.util.Calendar;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                Calendar value = entry.getDeleteAt();
                Log.d("Contentstack", "deleted_at: " + (value != null ? value.getTime() : "not set"));
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getDeletedBy

getDeletedBy returns the UID of the user who deleted the entry.

Returns: String. The UID of the user who deleted the entry.

Validation

  • There is no client-side validation. getDeletedBy does not throw.

Behavior

  • getDeletedBy reads the deleted_by value of the entry with getString.
  • It returns null when the entry has no deleted_by key.
  • Call it after fetch succeeds, or after configure. Before that, the entry holds no data and getDeletedBy returns null.

Example

Basic usage: read a value with getDeletedBy

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                Log.d("Contentstack", "deleted_by: " + entry.getDeletedBy());
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getDouble

getDouble returns the value of a field as a double.

NameTypeDescription
key (required)String

UID of the field to read.

Default: Not applicable

Returns: double. The field value as a double, or 0.

Validation

  • There is no client-side validation. getDouble does not throw.
  • A null key returns 0.

Behavior

  • getDouble returns 0 when the field is missing or its value is not a JSON number. A numeric string also returns 0.
  • Call it after fetch succeeds, or after configure. Before that, the entry holds no data and getDouble returns 0.

Example

Basic usage: read a value with getDouble

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                double price = entry.getDouble("price");
                Log.d("Contentstack", "Price: " + price);
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getFloat

getFloat returns the value of a field as a float.

NameTypeDescription
key (required)String

UID of the field to read.

Default: Not applicable

Returns: float. The field value as a float, or 0.

Validation

  • There is no client-side validation. getFloat does not throw.
  • A null key returns 0.

Behavior

  • getFloat returns 0 when the field is missing or its value is not a JSON number. A numeric string also returns 0.
  • It converts any number with floatValue(), which can lose precision.
  • Call it after fetch succeeds, or after configure. Before that, the entry holds no data and getFloat returns 0.

Example

Basic usage: read a value with getFloat

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                float weight = entry.getFloat("weight_kg");
                Log.d("Contentstack", "Weight: " + weight);
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getGroup

getGroup returns a Group field as a Group object.

NameTypeDescription
key (required)String

UID of the Group field.

Default: Not applicable

Returns: Group. The Group field as a Group object, or null.

Validation

  • There is no client-side validation.
  • getGroup throws NullPointerException when the entry holds no data yet. Call it after fetch succeeds or after configure.

Behavior

  • getGroup returns null when key is null or empty, or when the field is missing or does not hold a JSON object.
  • Read the subfields with the Group getters, which match the field getters of this class.
  • For a Group field marked Multiple, use the getGroups method.

Example

Basic usage: read a value with getGroup

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Group;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                Group seo = entry.getGroup("seo");
                if (seo != null) {
                    Log.d("Contentstack", "Meta title: " + seo.getString("meta_title"));
                }
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getGroups

getGroups returns each item of a multiple Group field as a Group object.

NameTypeDescription
key (required)String

UID of the multiple Group field.

Default: Not applicable

Returns: List<Group>. The Group field's items as Group objects, or null.

Validation

  • There is no client-side validation.
  • getGroups throws NullPointerException when the entry holds no data yet. Call it after fetch succeeds or after configure.

Behavior

  • getGroups returns null when key is null or empty, or when the field is missing or does not hold a JSON array. It skips an item that is not a JSON object.
  • Read the subfields with the Group getters.
  • For a Group field that holds one item, use the getGroup method.

Example

Basic usage: read a value with getGroups

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Group;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import java.util.List;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                List<Group> faqs = entry.getGroups("faqs");
                if (faqs != null) {
                    for (Group faq : faqs) {
                        Log.d("Contentstack", "Question: " + faq.getString("question"));
                    }
                }
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getHeaders

getHeaders returns the headers that this entry set.

Returns: ArrayMap. The headers set on this entry.

Validation

  • There is no client-side validation. getHeaders cannot fail.

Behavior

  • The map is an android.util.ArrayMap<String, Object>. It holds only the headers of this entry, which setHeader and variants write. It does not hold the stack headers, such as api_key, access_token and environment.
  • The map is empty when the entry set no header.
  • getHeaders returns the map that the entry uses, not a copy. A change to the map changes the headers that fetch sends.

Example

Basic usage: list the headers of an entry

import android.util.ArrayMap;
import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>")
            .variants("<VARIANT_UID>", "<BRANCH>");
    ArrayMap<String, Object> headers = entry.getHeaders();
    // Logs the x-cs-variant-uid and branch headers
    Log.d("Contentstack", "Entry headers: " + headers);
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getHtmlText

getHtmlText returns the value of a Markdown field rendered as HTML.

NameTypeDescription
markdownKey (required)String

UID of the Markdown field.

Default: Not applicable

Returns: String. The Markdown field rendered as HTML, or null.

Validation

  • There is no client-side validation. getHtmlText does not throw.
  • A null key returns null.

Behavior

  • getHtmlText converts the Markdown with the txtmark library (com.github.rjeschke:txtmark 0.12), using its extended profile.
  • It returns null when the field is missing or its value is not a string.
  • For a Markdown field that holds several values, use the getMultipleHtmlText method.
  • Call it after fetch succeeds, or after configure. Before that, the entry holds no data and getHtmlText returns null.

Example

Basic usage: read a value with getHtmlText

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                String html = entry.getHtmlText("body");
                Log.d("Contentstack", "HTML: " + html);
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getInt

getInt returns the value of a field as an int.

NameTypeDescription
key (required)String

UID of the field to read.

Default: Not applicable

Returns: int. The field value as an int, or 0.

Validation

  • There is no client-side validation. getInt does not throw.
  • A null key returns 0.

Behavior

  • getInt returns 0 when the field is missing or its value is not a JSON number. A numeric string such as "5" also returns 0.
  • It converts any number with intValue(), so it drops a decimal part and wraps a value outside the int range.
  • Call it after fetch succeeds, or after configure. Before that, the entry holds no data and getInt returns 0.

Example

Basic usage: read a value with getInt

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                int stock = entry.getInt("stock_count");
                Log.d("Contentstack", "Stock: " + stock);
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getJSONArray

getJSONArray returns the value of a field as a JSONArray.

NameTypeDescription
key (required)String

UID of the field to read.

Default: Not applicable

Returns: JSONArray. The field value as a JSONArray, or null.

Validation

  • There is no client-side validation. getJSONArray does not throw.
  • A null key returns null.

Behavior

  • getJSONArray returns null when the field is missing or its value is not a JSON array.
  • Multiple fields, Modular Blocks and reference fields hold arrays.
  • Call it after fetch succeeds, or after configure. Before that, the entry holds no data and getJSONArray returns null.

Example

Basic usage: read a value with getJSONArray

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import org.json.JSONArray;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                JSONArray keywords = entry.getJSONArray("keywords");
                Log.d("Contentstack", "Keywords: " + keywords);
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getJSONObject

getJSONObject returns the value of a field as a JSONObject.

NameTypeDescription
key (required)String

UID of the field to read.

Default: Not applicable

Returns: JSONObject. The field value as a JSONObject, or null.

Validation

  • There is no client-side validation. getJSONObject does not throw.
  • A null key returns null.

Behavior

  • getJSONObject returns null when the field is missing or its value is not a JSON object.
  • Group fields, file fields and JSON Rich Text Editor fields hold objects.
  • Call it after fetch succeeds, or after configure. Before that, the entry holds no data and getJSONObject returns null.

Example

Basic usage: read a value with getJSONObject

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import org.json.JSONObject;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                JSONObject seo = entry.getJSONObject("seo");
                Log.d("Contentstack", "SEO: " + seo);
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getLanguage

getLanguage returns the locale of the fetched entry as a Language value.

Returns: Language. The entry's locale as a Language value.

Validation

  • There is no client-side validation.
  • getLanguage throws IllegalArgumentException when the locale code of the entry has no Language value, because it looks the code up in a fixed list.
  • It throws NullPointerException when the entry holds no data yet.

Behavior

  • getLanguage reads the locale value of the _metadata object, or else of the entry JSON. It returns null when neither holds one.
  • getLocale returns the same locale as a String and accepts every locale code.

Example

Migrate to getLocale

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                // Before: Language language = entry.getLanguage();
                String locale = entry.getLocale();
                Log.d("Contentstack", "Locale: " + locale);
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getLocale

getLocale returns the locale code of the fetched entry.

Returns: String. The locale code of the fetched entry.

Validation

  • There is no client-side validation.
  • getLocale throws NullPointerException when the entry holds no data yet, because it reads the stored entry JSON. Call it after fetch succeeds or after configure.

Behavior

  • getLocale returns the locale value of the stored entry JSON, for example en-us.
  • With includeFallback, the value shows whether the API returned the requested locale or a fallback locale.
  • It replaces the deprecated getLanguage method.

Example

Basic usage: read a value with getLocale

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>")
            .setLocale("<LOCALE>")
            .includeFallback();
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                Log.d("Contentstack", "Locale: " + entry.getLocale());
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getLong

getLong returns the value of a field as a long.

NameTypeDescription
key (required)String

UID of the field to read.

Default: Not applicable

Returns: long. The field value as a long, or 0.

Validation

  • There is no client-side validation. getLong does not throw.
  • A null key returns 0.

Behavior

  • getLong returns 0 when the field is missing or its value is not a JSON number. A numeric string also returns 0.
  • It converts any number with longValue(), so it drops a decimal part.
  • Call it after fetch succeeds, or after configure. Before that, the entry holds no data and getLong returns 0.

Example

Basic usage: read a value with getLong

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                long views = entry.getLong("view_count");
                Log.d("Contentstack", "Views: " + views);
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getMultipleHtmlText

getMultipleHtmlText returns each value of a multiple Markdown field rendered as HTML.

NameTypeDescription
markdownKey (required)String

UID of the multiple Markdown field.

Default: Not applicable

Returns: ArrayList<String>. The Markdown values rendered as HTML, or null.

Validation

  • There is no client-side validation. getMultipleHtmlText does not throw.
  • A null key returns null.

Behavior

  • getMultipleHtmlText converts each item of the array with the txtmark library (com.github.rjeschke:txtmark 0.12), using its extended profile.
  • It returns null when the field is missing or its value is not an array.
  • For a Markdown field that holds one value, use the getHtmlText method.
  • Call it after fetch succeeds, or after configure. Before that, the entry holds no data and getMultipleHtmlText returns null.

Example

Basic usage: read a value with getMultipleHtmlText

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import java.util.ArrayList;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                ArrayList<String> sections = entry.getMultipleHtmlText("sections");
                if (sections != null) {
                    for (String html : sections) {
                        Log.d("Contentstack", "Section: " + html);
                    }
                }
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getNumber

getNumber returns the value of a field as a Number.

NameTypeDescription
key (required)String

UID of the field to read.

Default: Not applicable

Returns: Number. The field value as a Number, or null.

Validation

  • There is no client-side validation. getNumber does not throw.
  • A null key returns null.

Behavior

  • getNumber returns null when the field is missing or its value is not a JSON number.
  • The object is an Integer, Long or Double, depending on the stored value.
  • Call it after fetch succeeds, or after configure. Before that, the entry holds no data and getNumber returns null.

Example

Basic usage: read a value with getNumber

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                Number price = entry.getNumber("price");
                Log.d("Contentstack", "Price: " + price);
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getOwner

getOwner returns the owner details of the entry.

Returns: HashMap. The entry's owner details, or null.

Validation

  • There is no client-side validation. getOwner does not throw.

Behavior

  • getOwner returns a HashMap<String, Object> that holds the keys of the _owner object of the entry JSON. The SDK stores each value as a String.
  • It returns null when the entry JSON has no _owner key.
  • Call it after fetch succeeds, or after configure. Before that, the entry holds no data and getOwner returns null.

Example

Basic usage: read a value with getOwner

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import java.util.HashMap;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                HashMap<String, Object> owner = entry.getOwner();
                Log.d("Contentstack", "Owner: " + (owner != null ? owner.get("uid") : "not set"));
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getShort

getShort returns the value of a field as a short.

NameTypeDescription
key (required)String

UID of the field to read.

Default: Not applicable

Returns: short. The field value as a short, or 0.

Validation

  • There is no client-side validation. getShort does not throw.
  • A null key returns 0.

Behavior

  • getShort returns 0 when the field is missing or its value is not a JSON number. A numeric string also returns 0.
  • It converts any number with shortValue(), so it drops a decimal part and wraps a value outside the short range.
  • Call it after fetch succeeds, or after configure. Before that, the entry holds no data and getShort returns 0.

Example

Basic usage: read a value with getShort

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                short rating = entry.getShort("rating");
                Log.d("Contentstack", "Rating: " + rating);
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getString

getString returns the value of a field as a String.

NameTypeDescription
key (required)String

UID of the field to read.

Default: Not applicable

Returns: String. The field value as a String, or null.

Validation

  • There is no client-side validation. getString does not throw.
  • A null key returns null.

Behavior

  • getString returns null when the field is missing or its value is not a JSON string. It does not convert a number or a Boolean to text.
  • Call it after fetch succeeds, or after configure. Before that, the entry holds no data and getString returns null.

Example

Basic usage: read a value with getString

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                String summary = entry.getString("summary");
                Log.d("Contentstack", "Summary: " + summary);
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getTags

getTags returns the tags of the entry.

Returns: String[]. The entry's tags, or null.

Validation

  • There is no client-side validation. getTags does not throw.

Behavior

  • getTags returns null, not an empty array, when the entry has no tags. Check for null before you loop.
  • Call it after fetch succeeds, or after configure. Before that, the entry holds no data and getTags returns null.

Example

Basic usage: read a value with getTags

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                String[] tags = entry.getTags();
                if (tags != null) {
                    for (String tag : tags) {
                        Log.d("Contentstack", "Tag: " + tag);
                    }
                }
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getTitle

getTitle returns the title of the entry.

Returns: String. The entry's title.

Validation

  • There is no client-side validation. getTitle does not throw.

Behavior

  • getTitle returns the title value that fetch or configure stored. It returns null when the entry has no title key, and a single space when the value is JSON null.
  • Entries from the getAllEntries method hold no stored title, so getTitle returns null for them. Call getString("title") instead.
  • Call it after fetch succeeds, or after configure. Before that, the entry holds no data and getTitle returns null.

Example

Basic usage: read a value with getTitle

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                Log.d("Contentstack", "Title: " + entry.getTitle());
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getURL

getURL returns the value of the url field of the entry.

Returns: String. The entry's URL field value.

Validation

  • There is no client-side validation. getURL does not throw.

Behavior

  • getURL returns the url value that fetch or configure stored. It returns null when the entry has no url key, and a single space when the value is JSON null.
  • Entries from the getAllEntries method hold no stored URL, so getURL returns null for them. Call getString("url") instead.
  • Call it after fetch succeeds, or after configure. Before that, the entry holds no data and getURL returns null.

Example

Basic usage: read a value with getURL

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                Log.d("Contentstack", "URL: " + entry.getURL());
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getUid

getUid returns the UID of the entry.

Returns: String. The entry's UID.

Validation

  • There is no client-side validation. getUid does not throw.

Behavior

  • Before a request, getUid returns the UID you passed to entry("<ENTRY_UID>").
  • After fetch or configure, it returns the uid of the stored entry JSON.

Example

Basic usage: read a value with getUid

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                Log.d("Contentstack", "UID: " + entry.getUid());
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getUpdateAt

getUpdateAt returns the last update time of the entry as a Calendar.

Returns: Calendar. The entry's last update time, or null.

Validation

  • There is no client-side validation. getUpdateAt does not throw.

Behavior

  • getUpdateAt parses the updated_at value of the entry, the same way getDate does. It accepts ISO 8601 date-time values and date-only values such as 2024-05-10.
  • It returns null when the entry has no updated_at key, or when the value matches no accepted format.
  • Call it after fetch succeeds, or after configure. Before that, the entry holds no data and getUpdateAt returns null.
  • Known issue: getUpdateAt reads a UTC date-time value, such as 2024-05-10T10:24:13.000Z, as device-local time, so the returned Calendar is off by the UTC offset of the device. Read the value with getString and parse it with a formatter set to the UTC time zone instead.

Example

Basic usage: read a value with getUpdateAt

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import java.util.Calendar;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                Calendar value = entry.getUpdateAt();
                Log.d("Contentstack", "updated_at: " + (value != null ? value.getTime() : "not set"));
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getUpdatedAt

getUpdatedAt returns the value of a field as a String.

NameTypeDescription
key (required)String

UID of the field to read.

Default: Not applicable

Returns: String. The field value as a String, or null.

Validation

  • There is no client-side validation. getUpdatedAt does not throw.
  • A null key returns null.

Behavior

  • getUpdatedAt reads the field that key names, whatever its name. It returns the same value as getString.
  • To read the updated_at time as a Calendar, use the getUpdateAt method.
  • getUpdatedAt returns null when the field is missing or its value is not a JSON string.
  • Call it after fetch succeeds, or after configure. Before that, the entry holds no data and getUpdatedAt returns null.

Example

Basic usage: read a value with getUpdatedAt

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                String updatedAt = entry.getUpdatedAt("updated_at");
                Log.d("Contentstack", "Updated at: " + updatedAt);
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

getUpdatedBy

getUpdatedBy returns the UID of the user who last updated the entry.

Returns: String. The UID of the user who last updated the entry.

Validation

  • There is no client-side validation. getUpdatedBy does not throw.

Behavior

  • getUpdatedBy reads the updated_by value of the entry with getString.
  • It returns null when the entry has no updated_by key.
  • Call it after fetch succeeds, or after configure. Before that, the entry holds no data and getUpdatedBy returns null.

Example

Basic usage: read a value with getUpdatedBy

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                Log.d("Contentstack", "updated_by: " + entry.getUpdatedBy());
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

includeContentType

includeContentType asks the API to return the content type schema with the entry.

Returns: Entry. The same entry, for chaining.

Validation

  • There is no client-side validation.
  • An invalid value surfaces as an Error in the callback when fetch sends the request.

Additional Resource Refer to Content Delivery API Errors for the full error code list.

Behavior

  • Client-side only. includeContentType records include_content_type=true and include_global_field_schema=true, and fetch sends both. The second parameter asks for the schema of the Global fields in the content type.
  • It removes an include_schema parameter that the addParam method recorded.
  • fetch stores only the entry object of the response body, and toJSON returns that object. No Entry getter returns another key of the body.
  • A second call changes nothing. The value stays on the entry for every later fetch. No method removes it, so create a new entry to send a request without it.

Limitations

  • Does not expose the schema through any Entry getter. Fetch the schema with the ContentType class instead.

Example

Basic usage: request the content type with the entry

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>")
            .includeContentType();
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                Log.d("Contentstack", "Entry: " + entry.toJSON());
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

includeEmbeddedItems

includeEmbeddedItems returns the entries and assets embedded in the Rich Text Editor fields of the entry.

Returns: Entry. The same entry, for chaining.

Validation

  • There is no client-side validation.
  • An invalid value surfaces as an Error in the callback when fetch sends the request.

Additional Resource Refer to Content Delivery API Errors for the full error code list.

Behavior

  • Client-side only. includeEmbeddedItems records include_embedded_items[]=BASE, and fetch sends it. The API then returns the first level of embedded entries and assets under the _embedded_items key, as Get information on embedded RTE objects describes.
  • Read the embedded items with getJSONObject, passing _embedded_items.
  • To render a JSON Rich Text Editor node as HTML, use the NodeToHTML class.
  • A second call changes nothing. The value stays on the entry for every later fetch. No method removes it, so create a new entry to send a request without it.

Limitations

  • Does not return items embedded inside embedded items. Call the addParam method with include_embedded_items[] set to RECURSIVE instead, as the second example below shows.

Example

Basic usage: return embedded items

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>")
            .includeEmbeddedItems();
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                Log.d("Contentstack", "Embedded items: " + entry.getJSONObject("_embedded_items"));
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

Edge case: embedded items nested inside embedded items

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    // RECURSIVE replaces the BASE value under the same key. The API caps the depth at 5.
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>")
            .addParam("include_embedded_items[]", "RECURSIVE")
            .addParam("embedded_items_depth", "3");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error != null) {
                Log.e("Contentstack", error.getErrorMessage());
                return;
            }
            Log.d("Contentstack", "Embedded items: " + entry.getJSONObject("_embedded_items"));
        }
    });
} catch (Exception e) {
    Log.e("Contentstack", e.getMessage());
}

includeFallback

includeFallback returns the entry from the fallback locale when it has no published version in the requested locale.

Returns: Entry. The same entry, for chaining.

Validation

  • There is no client-side validation.
  • An invalid value surfaces as an Error in the callback when fetch sends the request.

Additional Resource Refer to Content Delivery API Errors for the full error code list.

Behavior

  • Client-side only. includeFallback records include_fallback=true, and fetch sends it. With it, the API returns the published content of the fallback locale when the requested locale has none, as Get a single entry describes.
  • Pair it with the setLocale method. Read getLocale after the request to see which locale the API returned.
  • A second call changes nothing. The value stays on the entry for every later fetch. No method removes it, so create a new entry to send a request without it.

Example

Basic usage: fetch a locale or its fallback

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>")
            .setLocale("<LOCALE>")
            .includeFallback();
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                // getLocale shows whether the API returned the requested locale or a fallback
                Log.d("Contentstack", "Locale: " + entry.getLocale() + ", title: " + entry.getTitle());
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

includeMetadata

includeMetadata returns the metadata of the entry under the _metadata key.

Returns: Entry. The same entry, for chaining.

Validation

  • There is no client-side validation.
  • An invalid value surfaces as an Error in the callback when fetch sends the request.

Additional Resource Refer to Content Delivery API Errors for the full error code list.

Behavior

  • Client-side only. includeMetadata records include_metadata=true, and fetch sends it. The API then adds the _metadata key, which holds the metadata of each extension under its extension UID, as Get a single entry describes.
  • Read the metadata with getJSONObject, passing _metadata.
  • A second call changes nothing. The value stays on the entry for every later fetch. No method removes it, so create a new entry to send a request without it.

Example

Basic usage: return the entry metadata

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>")
            .includeMetadata();
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                Log.d("Contentstack", "Metadata: " + entry.getJSONObject("_metadata"));
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

includeReference

includeReference returns the content of referenced entries in place of their UIDs.

NameTypeDescription
referenceField (required)String

UID of the reference field to include.

Default: Not applicable
referenceFields (required)String[]

UIDs of the reference fields to include.

Default: Not applicable

Returns: Entry. The same entry, for chaining.

Overloads

  • includeReference(String referenceField)
  • includeReference(String[] referenceFields)

Validation

  • There is no client-side validation. The String overload ignores a null or empty value. The String[] overload ignores a null or empty array, and does not check the items.
  • An invalid value surfaces as an Error in the callback when fetch sends the request.

Additional Resource Refer to Content Delivery API Errors for the full error code list.

Behavior

  • Client-side only. includeReference records the UIDs, and fetch sends each one as an include[] query parameter. Without it, the API returns only the UIDs of the referenced entries, as Include Reference describes.
  • Calls add up. A second call appends its UIDs to the list.
  • For a reference field inside a Group field, pass group_uid.reference_field_uid. Inside a block, pass modular_block_uid.block_uid.reference_field_uid.
  • Read the referenced entries with the getAllEntries method, or as JSON with getJSONArray.
  • The value stays on the entry for every later fetch. No method removes it, so create a new entry to send a request without it.

Limitations

  • Does not include every reference field at once. Call the addParam method with include_all set to true, and include_all_depth set to a value from 1 to 5, as Include all references describes.

Example

Basic usage: include one reference field

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import java.util.ArrayList;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>")
            .includeReference("authors");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                ArrayList<Entry> authors = entry.getAllEntries("authors", "author");
                if (authors != null) {
                    for (Entry author : authors) {
                        Log.d("Contentstack", "Author: " + author.getString("title"));
                    }
                }
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

Several reference fields in one call

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>")
            .includeReference(new String[]{"authors", "categories", "seo.related_pages"});
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error != null) {
                Log.e("Contentstack", error.getErrorMessage());
                return;
            }
            Log.d("Contentstack", "Categories: " + entry.getJSONArray("categories"));
        }
    });
} catch (Exception e) {
    Log.e("Contentstack", e.getMessage());
}

includeReferenceContentTypeUID

includeReferenceContentTypeUID asks the API to return the content type UID of each referenced entry.

Returns: Entry. The same entry, for chaining.

Validation

  • There is no client-side validation.
  • An invalid value surfaces as an Error in the callback when fetch sends the request.

Additional Resource Refer to Content Delivery API Errors for the full error code list.

Behavior

  • Client-side only. includeReferenceContentTypeUID records include_reference_content_type_uid=true, and fetch sends it.
  • It affects only the references that the response includes. Pair it with the includeReference method.
  • The content type UID helps when one reference field can point to entries of several content types, for example before you call getAllEntries.
  • A second call changes nothing. The value stays on the entry for every later fetch. No method removes it, so create a new entry to send a request without it.

Example

Basic usage: return content type UIDs with the references

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>")
            .includeReference("related_pages")
            .includeReferenceContentTypeUID();
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                Log.d("Contentstack", "Related pages: " + entry.getJSONArray("related_pages"));
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

only

only limits the response to the top-level fields you name.

NameTypeDescription
fieldUid (required)String[]

UIDs of the fields to return.

Default: Not applicable

Returns: Entry. The same entry, for chaining.

Validation

  • There is no client-side validation. only ignores a null or empty array.
  • An invalid value surfaces as an Error in the callback when fetch sends the request.

Additional Resource Refer to Content Delivery API Errors for the full error code list.

Behavior

  • Client-side only. only records the field UIDs and makes no request. fetch sends each one as an only[BASE][] query parameter, as the Only operator describes.
  • A second call adds its UIDs to the list.
  • For a field inside a Group field, pass the path with a dot, for example group_uid.field_uid. For a field inside a block, pass modular_block_uid.block_uid.field_uid.
  • The list applies to the next fetch only. fetch clears it when it sends a network request, so call only again before each fetch.
  • To apply the same filter to the fields of a referenced entry, use the onlyWithReferenceUid method.

Example

Basic usage: return only some fields

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>")
            .only(new String[]{"title", "url", "summary"});
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                Log.d("Contentstack", "Fields: " + entry.toJSON().names());
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

Fields inside a Group field

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>")
            .only(new String[]{"title", "seo.meta_title"});
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error != null) {
                Log.e("Contentstack", error.getErrorMessage());
                return;
            }
            Log.d("Contentstack", "SEO group: " + entry.getJSONObject("seo"));
        }
    });
} catch (Exception e) {
    Log.e("Contentstack", e.getMessage());
}

onlyWithReferenceUid

onlyWithReferenceUid includes a referenced entry and limits it to the fields you name.

NameTypeDescription
fieldUid (required)ArrayList<String>

Referenced-entry fields to return.

Default: Not applicable
referenceFieldUid (required)String

UID of the reference field.

Default: Not applicable

Returns: Entry. The same entry, for chaining.

Validation

  • There is no client-side validation. When fieldUid or referenceFieldUid is null, onlyWithReferenceUid records nothing.
  • An invalid value surfaces as an Error in the callback when fetch sends the request.

Additional Resource Refer to Content Delivery API Errors for the full error code list.

Behavior

  • Client-side only. onlyWithReferenceUid records the field UIDs, and fetch sends each one as an only[<referenceFieldUid>][] query parameter, as the Only operator describes.
  • onlyWithReferenceUid also calls includeReference with referenceFieldUid, so you do not need a separate includeReference call for that field.
  • A second call for the same reference field replaces its list. Calls for different reference fields add up.
  • An empty fieldUid list includes the referenced entry and sends no only parameter for it.
  • The field list applies to the next fetch only, because fetch clears it when it sends a network request. The include[] value stays for later calls.
  • To filter the top-level fields of the entry itself, use the only method.

Example

Basic usage: filter the fields of a referenced entry

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import java.util.ArrayList;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    ArrayList<String> authorFields = new ArrayList<>();
    authorFields.add("title");
    authorFields.add("bio");
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>")
            .onlyWithReferenceUid(authorFields, "authors");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                Log.d("Contentstack", "Authors: " + entry.getJSONArray("authors"));
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

Filter the entry and its reference together

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import java.util.ArrayList;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    ArrayList<String> authorFields = new ArrayList<>();
    authorFields.add("title");
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>")
            .only(new String[]{"title", "authors"})
            .onlyWithReferenceUid(authorFields, "authors");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error != null) {
                Log.e("Contentstack", error.getErrorMessage());
                return;
            }
            Log.d("Contentstack", "Title: " + entry.getTitle() + ", authors: " + entry.getJSONArray("authors"));
        }
    });
} catch (Exception e) {
    Log.e("Contentstack", e.getMessage());
}

removeHeader

removeHeader removes a header that this entry set.

NameTypeDescription
key (required)String

Name of the header to remove.

Default: Not applicable

Returns: void. This method returns nothing.

Validation

  • There is no client-side validation. removeHeader ignores a null or empty key, and a key that the entry does not hold.

Behavior

  • Client-side only. removeHeader removes the header from the headers of this entry, which setHeader and variants write.
  • It cannot remove a header that the stack or the content type sends, for example api_key, access_token or environment.
  • removeHeader returns void, so call it as a separate statement, not in a chain.

Example

Basic usage: return to the base entry after a variant

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>")
            .variants("<VARIANT_UID>");
    // Drop the variant header so that fetch returns the base entry
    entry.removeHeader("x-cs-variant-uid");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                Log.d("Contentstack", "Base title: " + entry.getTitle());
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

setCachePolicy

setCachePolicy sets where fetch reads the entry from: the network, the disk cache of the SDK, or both.

NameTypeDescription
cachePolicy (required)CachePolicy

Source order for fetch. Unset means NETWORK_ONLY.

Default: Not applicable

Returns: void. This method returns nothing.

Validation

  • There is no client-side validation.
  • setCachePolicy accepts null. The next fetch then reports an Error with ResponseType.UNKNOWN in the callback and sends no request.

Behavior

  • The policy applies to this entry only.
  • Every successful network request writes the response to the disk cache, whatever the policy.
  • A cached copy counts as fresh for one minute after the SDK writes it. No Entry method changes this window.
  • Creating a stack with Contentstack.stack schedules a daily cleanup that deletes cached responses 24 hours old or older.
  • responseType in onCompletion tells you where the result came from: CACHE or NETWORK.
  • Known issue: The SDK names a cached copy after the request path and headers only, not the query parameters. Two fetches of one entry with different parameters, for example two locales from setLocale, share one cached copy, so a cache read can return the other request's response. Use NETWORK_ONLY for an entry that you fetch with changing parameters.

The table below lists what each CachePolicy value makes fetch do.

ValueWhat fetch does
NETWORK_ONLYDefault. Sends a network request.
IGNORE_CACHESends a network request, the same as NETWORK_ONLY.
CACHE_ONLYReturns the cached copy. Without a fresh copy, reports an Error with ResponseType.CACHE and sends no request.
CACHE_ELSE_NETWORKReturns the cached copy when it is fresh, else sends a network request.
CACHE_THEN_NETWORKReturns any cached copy first, then sends a network request. The callback fires twice when a cached copy exists.
NETWORK_ELSE_CACHESends a network request when the device is online, else returns the cached copy.

Example

Basic usage: use the cached copy when it is fresh

import android.util.Log;

import com.contentstack.sdk.CachePolicy;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.setCachePolicy(CachePolicy.CACHE_ELSE_NETWORK);
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                Log.d("Contentstack", "Source: " + responseType + ", title: " + entry.getTitle());
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

Show cached content first, then the network result

import android.util.Log;

import com.contentstack.sdk.CachePolicy;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.setCachePolicy(CachePolicy.CACHE_THEN_NETWORK);
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error != null) {
                Log.e("Contentstack", error.getErrorMessage());
                return;
            }
            // Fires once with CACHE when a cached copy exists, then once with NETWORK
            Log.d("Contentstack", "Source: " + responseType + ", title: " + entry.getTitle());
        }
    });
} catch (Exception e) {
    Log.e("Contentstack", e.getMessage());
}

setHeader

setHeader adds a request header to the requests of this entry.

NameTypeDescription
key (required)String

Name of the header.

Default: Not applicable
value (required)String

Value of the header.

Default: Not applicable

Returns: void. This method returns nothing.

Validation

  • There is no client-side validation. When key or value is null or empty, setHeader records nothing.

Behavior

  • Client-side only. setHeader records the header on this entry, and fetch sends it.
  • A header of this entry wins over a stack or content type header with the same name. For example, setHeader("branch", ...) replaces the stack branch for this entry only.
  • A second call with the same key replaces the value.
  • setHeader returns void, so call it as a separate statement, not in a chain.
  • getHeaders lists the headers of this entry. removeHeader removes one.

Example

Basic usage: read the entry from another branch

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.setHeader("branch", "<BRANCH>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                Log.d("Contentstack", "Title on branch: " + entry.getTitle());
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

setLocale

setLocale sets the locale of the entry version that fetch retrieves.

NameTypeDescription
locale (required)String

Locale code, for example fr-fr.

Default: Not applicable

Returns: Entry. The same entry, for chaining.

Validation

  • There is no client-side validation. setLocale ignores null and does not check the format.
  • An invalid value surfaces as an Error in the callback when fetch sends the request.

Additional Resource Refer to Content Delivery API Errors for the full error code list.

Behavior

  • Client-side only. setLocale records the locale query parameter, and fetch sends it. The API returns only the version published in that locale, as Get a single entry describes.
  • Without a call, the SDK sends no locale and the API applies its own default.
  • A second call replaces the value. A call to the addParam method with the key locale replaces it too.
  • The value stays on the entry for every later fetch.
  • Add the includeFallback method to receive the fallback locale when the entry has no version in the requested one.

Example

Basic usage: fetch the entry in one locale

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>")
            .setLocale("<LOCALE>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                Log.d("Contentstack", "Locale: " + entry.getLocale() + ", title: " + entry.getTitle());
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

toJSON

toJSON returns every field of the entry as one JSON object.

Returns: JSONObject. The entry's fields as a JSON object.

Validation

  • There is no client-side validation. toJSON does not throw.

Behavior

  • toJSON returns the entry object of the response, not the whole response body.
  • It returns the object that the getters read, not a copy. A change to the object changes what the getters return.
  • Call it after fetch succeeds, or after configure. Before that, the entry holds no data and toJSON returns null.

Example

Basic usage: read a value with toJSON

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

import org.json.JSONObject;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                JSONObject json = entry.toJSON();
                Log.d("Contentstack", "Entry: " + json);
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

variants

variants fetches an entry variant by sending variant UIDs or aliases in the x-cs-variant-uid header.

NameTypeDescription
variants (required)String

Variant UID or variant alias.

Default: Not applicable
variantUid (required)String

Variant UID or variant alias.

Default: Not applicable
branchString

Branch UID for this entry's request.

Default: Not sent by this entry
variantUids (required)String[]

Variant UIDs or aliases, highest priority first.

Default: Not applicable

Returns: Entry. The same entry, for chaining.

Overloads

  • variants(String variants)
  • variants(String[] variants)
  • variants(String variantUid, String branch)
  • variants(String[] variantUids, String branch)

Validation

  • There is no client-side validation. variants trims each value and skips a null or blank one. When no value remains, it leaves the header as it was.
  • In the branch overloads, a null or blank branch sets no branch header.
  • An invalid value surfaces as an Error in the callback when fetch sends the request.

Additional Resource Refer to Content Delivery API Errors for the full error code list.

Behavior

  • Client-side only. variants writes the x-cs-variant-uid header on this entry, and fetch sends it with a Get single entry variant request.
  • The array overloads join the values with a comma and a space.
  • The API accepts variant UIDs and Personalize variant aliases. By default it accepts up to five values, and the first value takes priority for the base entry fields, as Get single entry variant describes.
  • A second call replaces the header value.
  • The branch overloads also write a branch header on this entry. It replaces, for this entry only, the branch that Config.setBranch set on the stack. The overloads write a non-blank branch even when every variant value was blank.
  • getHeaders shows both headers. removeHeader with x-cs-variant-uid returns the entry to the base version.

Example

Basic usage: fetch one variant

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>")
            .variants("<VARIANT_UID>");
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error == null) {
                Log.d("Contentstack", "Variant title: " + entry.getTitle());
            } else {
                // The API rejected the request or the network failed
                Log.e("Contentstack", "Request failed: " + error.getStatusCode() + " "
                        + error.getErrorCode() + " " + error.getErrorMessage());
            }
        }
    });
} catch (Exception e) {
    // Contentstack.stack throws when a credential is null
    Log.e("Contentstack", "Setup failed: " + e.getMessage());
}

Several variants, highest priority first

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    // A variant UID and a Personalize variant alias. The first value takes priority.
    Entry entry = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>")
            .variants(new String[]{"<VARIANT_UID>", "cs_personalize_0_1"});
    entry.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error != null) {
                Log.e("Contentstack", error.getErrorMessage());
                return;
            }
            Log.d("Contentstack", "Variant title: " + entry.getTitle());
        }
    });
} catch (Exception e) {
    Log.e("Contentstack", e.getMessage());
}

Variants on a branch

import android.util.Log;

import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.EntryResultCallBack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;

try {
    Config config = new Config();
    config.setHost("cdn.contentstack.io"); // your region's host, see Config setHost
    Stack stack = Contentstack.stack(context, "<API_KEY>", "<DELIVERY_TOKEN>", "<ENVIRONMENT>", config);
    // One variant, scoped to a branch
    Entry single = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>")
            .variants("<VARIANT_UID>", "<BRANCH>");
    // Several variants, scoped to the same branch
    Entry several = stack.contentType("<CONTENT_TYPE_UID>").entry("<ENTRY_UID>")
            .variants(new String[]{"<VARIANT_UID>", "cs_personalize_0_1"}, "<BRANCH>");
    single.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error != null) {
                Log.e("Contentstack", error.getErrorMessage());
                return;
            }
            Log.d("Contentstack", "Single variant: " + single.getTitle());
        }
    });
    several.fetch(new EntryResultCallBack() {
        @Override
        public void onCompletion(ResponseType responseType, Error error) {
            if (error != null) {
                Log.e("Contentstack", error.getErrorMessage());
                return;
            }
            Log.d("Contentstack", "Several variants: " + several.getTitle());
        }
    });
} catch (Exception e) {
    Log.e("Contentstack", e.getMessage());
}