Stack
Stack
Stack represents one Contentstack stack and environment. Every other object in the SDK comes from it. Use this class to
- open content types, assets, asset lists, global fields, and taxonomies
- synchronize published content through the Sync API
- list the content types of the stack
- add, change, or remove request headers
- build an image URL with transformation parameters
Create a stack with the stack method of Contentstack. The Stack constructors are protected, so app code cannot call new Stack().
ImageTransform
ImageTransform appends Image Delivery API parameters to an image URL, so the image loads resized, cropped, or converted.
| Name | Type | Description |
|---|---|---|
| image_url (required) | String | Image URL to modify, such as an asset URL. |
| parameters | LinkedHashMap<String, Object> | Transformation parameter names and values to append. |
Returns: String. The image URL followed by the transformation query parameters.
Validation
- There is no client-side validation of parameter names or values. ImageTransform appends a name the Image Delivery API does not support without an error.
- ImageTransform throws NullPointerException when a value in parameters is null, because it converts each value with toString(). It also throws when image_url is null and parameters holds at least one pair.
Behavior
- Client-side only. ImageTransform builds a string and makes no request. The Image Delivery API applies the transformation when your app loads the URL.
- ImageTransform appends each pair as name=value, in the order you added them to parameters. It starts with ? when the URL has no query string, and with & otherwise.
- ImageTransform converts each value with toString() and URL-encodes each name and value as UTF-8. A comma in a value, as in a crop value, reaches the URL as %2C.
- A null or empty parameters returns image_url unchanged.
- ImageTransform adds only the pairs you pass. It does not add the environment parameter or any other value from the stack.
- The Image Delivery API reference lists every parameter, for example width, height, format, and quality.
Example
Basic usage: resize a fetched asset
import android.util.Log;
import com.contentstack.sdk.Asset;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.FetchResultCallback;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;
import java.util.LinkedHashMap;
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);
Asset asset = stack.asset("<ASSET_UID>");
asset.fetch(new FetchResultCallback() {
@Override
public void onCompletion(ResponseType responseType, Error error) {
if (error == null) {
LinkedHashMap<String, Object> parameters = new LinkedHashMap<>();
parameters.put("width", 300);
parameters.put("height", 200);
String imageUrl = stack.ImageTransform(asset.getUrl(), parameters);
Log.d("Contentstack", imageUrl);
} 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());
}Convert the format: serve a WebP image at a lower quality
import android.util.Log;
import com.contentstack.sdk.Asset;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.FetchResultCallback;
import com.contentstack.sdk.ResponseType;
import com.contentstack.sdk.Stack;
import java.util.LinkedHashMap;
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);
Asset asset = stack.asset("<ASSET_UID>");
asset.fetch(new FetchResultCallback() {
@Override
public void onCompletion(ResponseType responseType, Error error) {
if (error != null) {
Log.e("Contentstack", error.getErrorMessage());
return;
}
LinkedHashMap<String, Object> parameters = new LinkedHashMap<>();
parameters.put("format", "webp");
parameters.put("quality", 80);
Log.d("Contentstack", stack.ImageTransform(asset.getUrl(), parameters));
}
});
} catch (Exception e) {
Log.e("Contentstack", e.getMessage());
}asset
asset creates an Asset object for one asset UID, so you can fetch that asset.
| Name | Type | Description |
|---|---|---|
| uid (required) | String | UID of the asset to fetch. |
Returns: Asset. A new asset object.
Validation
- There is no client-side validation. asset stores the UID as given, including null or an empty string.
- An invalid UID surfaces as an Error in the callback when the fetch method of Asset sends the request.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- Client-side only. asset makes no request.
- The asset reads the header map of the stack, so headers you set on the stack apply to its requests.
- Call fetch on the returned object to read the asset. The Asset class page lists every method.
- Use the assetLibrary method to list assets instead of reading one.
Example
Basic usage: fetch one asset and read its URL
import android.util.Log;
import com.contentstack.sdk.Asset;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.FetchResultCallback;
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);
Asset asset = stack.asset("<ASSET_UID>");
asset.fetch(new FetchResultCallback() {
@Override
public void onCompletion(ResponseType responseType, Error error) {
if (error == null) {
Log.d("Contentstack", asset.getFileName() + " " + asset.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());
}assetLibrary
assetLibrary creates an AssetLibrary object, so you can list the assets published to the environment.
Returns: AssetLibrary. A new asset library object.
Validation
- There is no client-side validation. assetLibrary creates the object and sends no request.
Behavior
- Client-side only. assetLibrary makes no request.
- The asset library reads the header map of the stack, so headers you set on the stack apply to its requests.
- Chain filters such as includeCount on the returned object, then call fetchAll to send the request. The AssetLibrary class page lists every method.
- Use the asset method to read one asset by UID.
Example
Basic usage: list assets with the total count
import android.util.Log;
import com.contentstack.sdk.Asset;
import com.contentstack.sdk.AssetLibrary;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.FetchAssetsCallback;
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);
AssetLibrary assetLibrary = stack.assetLibrary().includeCount();
assetLibrary.fetchAll(new FetchAssetsCallback() {
@Override
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
if (error == null) {
Log.d("Contentstack", "Total assets: " + assetLibrary.getCount());
for (Asset asset : assets) {
Log.d("Contentstack", asset.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());
}contentType
contentType creates a ContentType object for one content type UID, so you can fetch its entries or its schema.
| Name | Type | Description |
|---|---|---|
| contentTypeName (required) | String | UID of the content type to open. |
Returns: ContentType. A new content type object.
Validation
- There is no client-side validation. contentType stores the UID as given, including null or an empty string.
- An invalid UID surfaces as an Error in a later callback. It arrives when a request method sends the request, such as the fetch method of Entry.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- Client-side only. contentType makes no request.
- The content type reads the header map of the stack, so headers you set on the stack apply to its requests. See Shared headers on the Stack class page.
- Call entry on the returned object to read one entry, query to find entries, or fetch to read the content type schema. The ContentType class page lists every method.
- Use the getContentTypes method to list every content type of the stack.
Example
Basic usage: fetch one entry of a content type
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", 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());
}Find entries: query every entry of a content type
import android.util.Log;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Entry;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.QueryResult;
import com.contentstack.sdk.QueryResultsCallBack;
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);
stack.contentType("<CONTENT_TYPE_UID>").query().find(new QueryResultsCallBack() {
@Override
public void onCompletion(ResponseType responseType, QueryResult queryresult, Error error) {
if (error != null) {
Log.e("Contentstack", error.getErrorMessage());
return;
}
for (Entry entry : queryresult.getResultObjects()) {
Log.d("Contentstack", entry.getTitle());
}
}
});
} catch (Exception e) {
Log.e("Contentstack", e.getMessage());
}getAccessToken
getAccessToken returns the value of the access_token header, which holds the delivery token the stack sends.
Returns: String. The delivery token the stack sends.
Validation
- There is no client-side validation. getAccessToken reads the current header map of the stack.
Behavior
- Client-side only. getAccessToken makes no request.
- getAccessToken reads the header at call time. After setHeader("access_token", ...) it returns the new value. After removeHeader("access_token") it returns null.
- Use the getApplicationKey method to read the API key.
Example
Basic usage: read the delivery token
import android.util.Log;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
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);
String deliveryToken = stack.getAccessToken();
Log.d("Contentstack", "Delivery token: " + deliveryToken);
} catch (Exception e) {
// Contentstack.stack throws when a credential is null
Log.e("Contentstack", "Setup failed: " + e.getMessage());
}getApplicationKey
getApplicationKey returns the API key you passed to Contentstack.stack.
Returns: String. The stack's API key.
Validation
- There is no client-side validation. getApplicationKey reads a value the stack stored when you created it.
Behavior
- Client-side only. getApplicationKey makes no request.
- The SDK trims leading and trailing whitespace from the key before it stores the value.
- The value does not change after you create the stack. A later setHeader("api_key", ...) or removeHeader("api_key") changes the header the SDK sends, not this value.
- Use the getAccessToken method to read the delivery token.
Example
Basic usage: read the API key
import android.util.Log;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
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);
String apiKey = stack.getApplicationKey();
Log.d("Contentstack", "API key: " + apiKey);
} catch (Exception e) {
// Contentstack.stack throws when a credential is null
Log.e("Contentstack", "Setup failed: " + e.getMessage());
}getContentTypes
getContentTypes retrieves the content types of the stack, with their schemas.
| Name | Type | Description |
|---|---|---|
| params | JSONObject | Query parameters to add to the request. |
| callback (required) | ContentTypesCallback | Receives the content types or the error. |
Returns: void. The result arrives in the callback.
Validation
- There is no client-side validation of params. The SDK adds each key and value to the request URL as written, without URL encoding. Encode a value that contains spaces or reserved characters before you pass it.
- A null callback makes getContentTypes send no request and report nothing.
- An API rejection or a network failure arrives as a non-null Error in the callback. getContentTypes does not throw for it.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- One call sends one Get all content types request, GET /content_types, with each key of params as a query parameter. A null params sends no parameters of your own.
- When the stack has an environment header, the SDK also sends environment and include_count=true. That value replaces any include_count you put in params, and the SDK writes both keys into the params object you passed.
- onCompletion(ContentTypesModel contentTypesModel, Error error) fires when the request ends. On success error is null. On failure contentTypesModel is null.
- contentTypesModel.getResultArray() returns a JSONArray with one JSONObject per content type. contentTypesModel.getResponse() returns an empty JSONObject on this call.
- The SDK does not expose the count value of the response.
- The SDK caches nothing for this call. Every call sends a request.
- Use the contentType method to open one content type by UID.
Example
Basic usage: list the content types of the stack
import android.util.Log;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.ContentTypesCallback;
import com.contentstack.sdk.ContentTypesModel;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Stack;
import org.json.JSONArray;
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);
stack.getContentTypes(null, new ContentTypesCallback() {
@Override
public void onCompletion(ContentTypesModel contentTypesModel, Error error) {
if (error == null) {
JSONArray contentTypes = contentTypesModel.getResultArray();
for (int i = 0; i < contentTypes.length(); i++) {
JSONObject contentType = contentTypes.optJSONObject(i);
Log.d("Contentstack", contentType.optString("uid") + " " + contentType.optString("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());
}All parameters: include global field schemas and the branch
import android.util.Log;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.ContentTypesCallback;
import com.contentstack.sdk.ContentTypesModel;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
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);
JSONObject params = new JSONObject();
params.put("include_global_field_schema", true);
params.put("include_branch", true);
stack.getContentTypes(params, new ContentTypesCallback() {
@Override
public void onCompletion(ContentTypesModel contentTypesModel, Error error) {
if (error != null) {
Log.e("Contentstack", error.getErrorMessage());
return;
}
Log.d("Contentstack", "Content types: " + contentTypesModel.getResultArray().length());
}
});
} catch (Exception e) {
Log.e("Contentstack", e.getMessage());
}globalField
globalField creates a GlobalField object for one global field, or for all global fields when you pass no UID.
| Name | Type | Description |
|---|---|---|
| global_field_uid | String | UID of the global field to fetch. |
Returns: GlobalField. A new global field object.
Validation
- There is no client-side validation. globalField stores the UID as given.
- The fetch method of GlobalField reports a null or empty UID as an Error in its callback without sending a request. An unknown UID surfaces as an Error from the API.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- Client-side only. globalField makes no request.
- The global field reads the header map of the stack, so headers you set on the stack apply to its requests.
- Call fetch on an object created with a UID to read that global field. Call findAll on an object created without a UID to list every global field. The Global Fields class page lists every method.
Example
Basic usage: fetch one global field by UID
import android.util.Log;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.GlobalField;
import com.contentstack.sdk.GlobalFieldsModel;
import com.contentstack.sdk.GlobalFieldsResultCallback;
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);
GlobalField globalField = stack.globalField("<GLOBAL_FIELD_UID>");
globalField.fetch(new GlobalFieldsResultCallback() {
@Override
public void onCompletion(GlobalFieldsModel globalFieldsModel, Error error) {
if (error == null) {
JSONObject schema = globalFieldsModel.getResponse();
Log.d("Contentstack", schema.optString("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());
}No UID: list every global field
import android.util.Log;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.GlobalFieldsModel;
import com.contentstack.sdk.GlobalFieldsResultCallback;
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);
stack.globalField().findAll(new GlobalFieldsResultCallback() {
@Override
public void onCompletion(GlobalFieldsModel globalFieldsModel, Error error) {
if (error != null) {
Log.e("Contentstack", error.getErrorMessage());
return;
}
JSONArray globalFields = globalFieldsModel.getResultArray();
for (int i = 0; i < globalFields.length(); i++) {
Log.d("Contentstack", globalFields.optJSONObject(i).optString("uid"));
}
}
});
} catch (Exception e) {
Log.e("Contentstack", e.getMessage());
}removeHeader
removeHeader removes one request header from the stack.
| Name | Type | Description |
|---|---|---|
| key (required) | String | Name of the header to remove. |
Returns: void. This method returns nothing.
Validation
- There is no client-side validation that reports an error. removeHeader ignores a null or empty key, and a key the stack does not hold.
Behavior
- Client-side only. removeHeader makes no request.
- The removal applies to every later request from the stack, and from the objects the stack created. See Shared headers on the Stack class page.
- removeHeader also removes the headers that Contentstack.stack set. Removing api_key or access_token makes every later request from the stack fail, with the API's rejection arriving as an Error in the callback.
- Use the setHeader method to add the header again.
Example
Basic usage: stop reading from a branch
import android.util.Log;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
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);
stack.setHeader("branch", "<BRANCH>");
// Later requests from this stack carry no branch header
stack.removeHeader("branch");
Log.d("Contentstack", "Branch header removed");
} catch (Exception e) {
// Contentstack.stack throws when a credential is null
Log.e("Contentstack", "Setup failed: " + e.getMessage());
}setHeader
setHeader adds a request header to the stack, or replaces the value of a header the stack already has.
| Name | Type | Description |
|---|---|---|
| key (required) | String | Name of the header to set. |
| value (required) | String | Value to send for that header. |
Returns: void. This method returns nothing.
Validation
- There is no client-side validation that reports an error. setHeader ignores the call when key or value is null or empty, so nothing in your code fails at that point.
- setHeader does not check the header name. A header the API does not use reaches the API unchanged.
Behavior
- Client-side only. setHeader makes no request.
- A key the stack already holds gets the new value. This includes the api_key, access_token, environment, and branch headers that Contentstack.stack sets.
- The header applies to every later request from the stack, and from the objects the stack created. See Shared headers on the Stack class page for the one exception.
- Use the setHeaders method to set several headers at once, and the removeHeader method to remove one.
Example
Basic usage: read entries from 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);
// Set the header before you create the entry
stack.setHeader("branch", "<BRANCH>");
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", 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());
}setHeaders
setHeaders adds several request headers to the stack in one call, replacing the value of any header the stack already has.
| Name | Type | Description |
|---|---|---|
| headers (required) | ArrayMap<String, String> | Header names and values to set. |
Returns: void. This method returns nothing.
Validation
- setHeaders throws NullPointerException when headers is null, because it reads every pair from the map.
- Unlike setHeader, setHeaders does not skip empty names or values. It adds every pair as given.
Behavior
- Client-side only. setHeaders makes no request.
- headers is an android.util.ArrayMap. setHeaders copies each pair into the header map of the stack. A key the stack already holds gets the new value.
- The headers apply to every later request from the stack, and from the objects the stack created. See Shared headers on the Stack class page.
- Use the removeHeader method to remove a header.
Example
Basic usage: set headers from a map
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.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);
ArrayMap<String, String> headers = new ArrayMap<>();
headers.put("branch", "<BRANCH>");
stack.setHeaders(headers);
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", 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());
}sync
sync starts an initial sync of the entries and assets published to the environment, either in full or narrowed by several filters at once.
| Name | Type | Description |
|---|---|---|
| syncCallBack (required) | SyncResultCallBack | Receives each page of the sync. |
| contentType | String | Content type UID to sync. null or empty sends none. |
| fromDate (required) | Date | Start date. Syncs content published from then. |
| locale | String | Locale code to sync. See the Known issue below. |
| type | PublishType | Event type to sync. null sends no type filter. |
| language | Language | Locale as a Language constant. See the Known issue below. |
Returns: void. The result arrives in the callback.
Overloads
- sync(SyncResultCallBack syncCallBack)
- sync(String contentType, Date fromDate, String locale, PublishType type, SyncResultCallBack syncCallBack)
- sync(String contentType, Date fromDate, Language language, PublishType type, SyncResultCallBack syncCallBack): Takes the same arguments as the overload above, with language in place of locale.
Validation
- There is no client-side validation of the content type UID, the locale, or the type.
- The multi-parameter overloads throw NullPointerException when fromDate is null, because the SDK formats the date before it builds the request.
- A bare null as the third argument does not compile, because both multi-parameter overloads accept it. Cast it, as in (String) null.
- An API rejection or a network failure arrives as a non-null Error in the callback. sync does not throw for it.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- Every overload sends an Initial Sync request, GET /stacks/sync, with init=true. sync(syncCallBack) sends no filter, so it syncs every entry and asset published to the environment.
- The multi-parameter overloads add these query parameters:
- content_type_uid when contentType is neither null nor empty
- start_from with fromDate in UTC, in the yyyy-MM-dd'T'HH:mm:ss.SSS'Z' format
- type with the lowercase name of type, for example entry_published, when type is not null. See syncPublishType for the seven values.
- Language is an SDK enum of locale constants. The SDK converts the constant to its locale code.
- Known issue: The multi-parameter overloads never send a non-empty locale or language, so the sync covers every locale. Use the syncLocale method for a sync of one locale.
- onCompletion(SyncStack syncStack, Error error) fires once per page. On success error is null and syncStack contains that page. On failure syncStack is null.
- The SDK requests every remaining page for you. The last page contains the sync token for the next syncToken call. See Synchronization on the Stack class page.
- Use syncContentType, syncFromDate, or syncPublishType when you need one filter only.
Example
Basic usage: sync everything published to the environment
import android.util.Log;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Stack;
import com.contentstack.sdk.SyncResultCallBack;
import com.contentstack.sdk.SyncStack;
import org.json.JSONObject;
import java.util.ArrayList;
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);
List<JSONObject> syncedItems = new ArrayList<>();
stack.sync(new SyncResultCallBack() {
@Override
public void onCompletion(SyncStack syncStack, Error error) {
if (error == null) {
// Called once per page
if (syncStack.getItems() != null) {
syncedItems.addAll(syncStack.getItems());
}
if (syncStack.getSyncToken() != null) {
// Last page: store this token for the next syncToken() call
Log.d("Contentstack", "Synced " + syncedItems.size() + " items. Sync token: "
+ syncStack.getSyncToken());
}
} 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 parameters: sync published entries of one content type from a date
import android.util.Log;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Stack;
import com.contentstack.sdk.SyncResultCallBack;
import com.contentstack.sdk.SyncStack;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
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);
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calendar.set(2026, Calendar.JANUARY, 1, 0, 0, 0);
calendar.set(Calendar.MILLISECOND, 0);
Date fromDate = calendar.getTime();
// The SDK does not send the locale argument. See the Known issue above.
stack.sync("<CONTENT_TYPE_UID>", fromDate, "<LOCALE>", Stack.PublishType.ENTRY_PUBLISHED,
new SyncResultCallBack() {
@Override
public void onCompletion(SyncStack syncStack, Error error) {
if (error != null) {
Log.e("Contentstack", error.getErrorMessage());
return;
}
if (syncStack.getItems() != null) {
Log.d("Contentstack", "Items in this page: " + syncStack.getItems().size());
}
}
});
} catch (Exception e) {
Log.e("Contentstack", e.getMessage());
}syncContentType
syncContentType starts an initial sync of the entries of one content type.
| Name | Type | Description |
|---|---|---|
| contentType (required) | String | UID of the content type to sync. |
| syncCallBack (required) | SyncResultCallBack | Receives each page of the sync. |
Returns: void. The result arrives in the callback.
Validation
- There is no client-side validation. syncContentType sends the UID as given.
- A null contentType sends no content_type_uid parameter, so the request becomes a full sync of every content type. Nothing in your code fails at that point. Check the value before you pass it.
- An API rejection or a network failure arrives as a non-null Error in the callback. syncContentType does not throw for it.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- syncContentType sends an Initial Sync request, GET /stacks/sync, with init=true and content_type_uid.
- onCompletion(SyncStack syncStack, Error error) fires once per page. On success error is null and syncStack contains that page. On failure syncStack is null.
- The SDK requests every remaining page for you. The last page contains the sync token for the next syncToken call. See Synchronization on the Stack class page.
- Use the sync method to combine the content type with a start date or a publish type.
Example
Basic usage: sync the entries of one content type
import android.util.Log;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Stack;
import com.contentstack.sdk.SyncResultCallBack;
import com.contentstack.sdk.SyncStack;
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);
stack.syncContentType("<CONTENT_TYPE_UID>", new SyncResultCallBack() {
@Override
public void onCompletion(SyncStack syncStack, Error error) {
if (error == null) {
// Called once per page
if (syncStack.getItems() != null) {
Log.d("Contentstack", "Items in this page: " + syncStack.getItems().size());
}
if (syncStack.getSyncToken() != null) {
// Last page: store this token for the next syncToken() call
Log.d("Contentstack", "Sync token: " + syncStack.getSyncToken());
}
} 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());
}syncFromDate
syncFromDate starts an initial sync of the content published from a given date onward.
| Name | Type | Description |
|---|---|---|
| fromDate (required) | Date | Start date. Syncs content published from then. |
| syncCallBack (required) | SyncResultCallBack | Receives each page of the sync. |
Returns: void. The result arrives in the callback.
Validation
- syncFromDate throws NullPointerException when fromDate is null, because the SDK formats the date before it builds the request.
- There is no other client-side validation. A date in the future reaches the API as given.
- An API rejection or a network failure arrives as a non-null Error in the callback. syncFromDate does not throw for it.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- syncFromDate sends an Initial Sync request, GET /stacks/sync, with init=true and start_from.
- The SDK converts fromDate to UTC in the yyyy-MM-dd'T'HH:mm:ss.SSS'Z' format, for example 2026-01-01T00:00:00.000Z. A Date holds an instant, so the device time zone does not change the value sent.
- onCompletion(SyncStack syncStack, Error error) fires once per page. On success error is null and syncStack contains that page. On failure syncStack is null.
- The SDK requests every remaining page for you. The last page contains the sync token for the next syncToken call. See Synchronization on the Stack class page.
- Use the sync method to combine the start date with a content type or a publish type.
Example
Basic usage: sync the content published since the start of 2026
import android.util.Log;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Stack;
import com.contentstack.sdk.SyncResultCallBack;
import com.contentstack.sdk.SyncStack;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
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);
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calendar.set(2026, Calendar.JANUARY, 1, 0, 0, 0);
calendar.set(Calendar.MILLISECOND, 0);
Date fromDate = calendar.getTime();
stack.syncFromDate(fromDate, new SyncResultCallBack() {
@Override
public void onCompletion(SyncStack syncStack, Error error) {
if (error == null) {
// Called once per page
if (syncStack.getItems() != null) {
Log.d("Contentstack", "Items in this page: " + syncStack.getItems().size());
}
if (syncStack.getSyncToken() != null) {
// Last page: store this token for the next syncToken() call
Log.d("Contentstack", "Sync token: " + syncStack.getSyncToken());
}
} 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());
}syncLocale
syncLocale starts an initial sync of the content published in one locale.
| Name | Type | Description |
|---|---|---|
| localeCode (required) | String | Locale code to sync, such as en-us. |
| syncCallBack (required) | SyncResultCallBack | Receives each page of the sync. |
| language (required) | Language | Locale to sync, as a Language constant. |
Returns: void. The result arrives in the callback.
Overloads
- syncLocale(String localeCode, SyncResultCallBack syncCallBack)
- syncLocale(Language language, SyncResultCallBack syncCallBack)
Validation
- There is no client-side validation. syncLocale sends the locale code as given.
- A null locale sends no locale parameter, so the request becomes a full sync of every locale. Nothing in your code fails at that point. Check the value before you pass it.
- A bare null as the first argument does not compile, because both overloads accept it.
- An API rejection or a network failure arrives as a non-null Error in the callback. syncLocale does not throw for it.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- syncLocale sends an Initial Sync request, GET /stacks/sync, with init=true and locale.
- Language is an SDK enum of locale constants. The Language overload converts the constant to its locale code and then behaves like the String overload.
- onCompletion(SyncStack syncStack, Error error) fires once per page. On success error is null and syncStack contains that page. On failure syncStack is null.
- The SDK requests every remaining page for you. The last page contains the sync token for the next syncToken call. See Synchronization on the Stack class page.
- syncLocale is the only sync method that sends a locale. The multi-parameter sync overloads do not send theirs.
Example
Basic usage: sync the content of one locale
import android.util.Log;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Stack;
import com.contentstack.sdk.SyncResultCallBack;
import com.contentstack.sdk.SyncStack;
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);
stack.syncLocale("<LOCALE>", new SyncResultCallBack() {
@Override
public void onCompletion(SyncStack syncStack, Error error) {
if (error == null) {
// Called once per page
if (syncStack.getItems() != null) {
Log.d("Contentstack", "Items in this page: " + syncStack.getItems().size());
}
if (syncStack.getSyncToken() != null) {
// Last page: store this token for the next syncToken() call
Log.d("Contentstack", "Sync token: " + syncStack.getSyncToken());
}
} 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());
}syncPaginationToken
syncPaginationToken resumes a sync from a pagination token, for example after a network failure stopped the sync partway.
| Name | Type | Description |
|---|---|---|
| paginationToken (required) | String | Pagination token from an earlier sync page. |
| syncCallBack (required) | SyncResultCallBack | Receives each page of the sync. |
Returns: void. The result arrives in the callback.
Validation
- There is no client-side validation. syncPaginationToken sends the token as given.
- A null token sends the request without a pagination_token parameter.
- An invalid or expired token, an API rejection, or a network failure arrives as a non-null Error in the callback. syncPaginationToken does not throw for it.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- syncPaginationToken sends a Sync using pagination token request, GET /stacks/sync, with pagination_token.
- The API returns a pagination token when a sync result holds more than 100 records. Each request with it returns the next batch.
- The other sync methods follow pagination tokens on their own, so you need syncPaginationToken only to resume a sync that stopped. Keep the last pagination token your callback received, from the getPaginationToken method of SyncStack, and pass it here.
- syncPaginationToken also follows every later pagination token, until the last page. It does not fetch one batch only.
- onCompletion(SyncStack syncStack, Error error) fires once per page. On success error is null and syncStack contains that page. On failure syncStack is null.
- The last page contains the sync token for the next syncToken call.
Example
Basic usage: resume an interrupted sync
import android.util.Log;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Stack;
import com.contentstack.sdk.SyncResultCallBack;
import com.contentstack.sdk.SyncStack;
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);
// The last pagination token your callback received before the sync stopped
stack.syncPaginationToken("<PAGINATION_TOKEN>", new SyncResultCallBack() {
@Override
public void onCompletion(SyncStack syncStack, Error error) {
if (error == null) {
// Called once per remaining page
if (syncStack.getItems() != null) {
Log.d("Contentstack", "Items in this page: " + syncStack.getItems().size());
}
if (syncStack.getSyncToken() != null) {
// Last page: store this token for the next syncToken() call
Log.d("Contentstack", "Sync token: " + syncStack.getSyncToken());
}
} 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());
}syncPublishType
syncPublishType starts an initial sync of one type of publishing event, such as published entries or deleted assets.
| Name | Type | Description |
|---|---|---|
| type (required) | PublishType | Event type to sync. |
| syncCallBack (required) | SyncResultCallBack | Receives each page of the sync. |
Returns: void. The result arrives in the callback.
Validation
- syncPublishType throws NullPointerException when type is null, because the SDK reads the name of the enum value before it builds the request.
- There is no other client-side validation.
- An API rejection or a network failure arrives as a non-null Error in the callback. syncPublishType does not throw for it.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- syncPublishType sends an Initial Sync request, GET /stacks/sync, with init=true and type.
- The API accepts several types as comma-separated values, but syncPublishType sends one. Run one sync per type.
- onCompletion(SyncStack syncStack, Error error) fires once per page. On success error is null and syncStack contains that page. On failure syncStack is null.
- The SDK requests every remaining page for you. The last page contains the sync token for the next syncToken call. See Synchronization on the Stack class page.
- Use the sync method to combine the type with a content type or a start date.
Example
Basic usage: sync the published entries
import android.util.Log;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Stack;
import com.contentstack.sdk.SyncResultCallBack;
import com.contentstack.sdk.SyncStack;
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);
stack.syncPublishType(Stack.PublishType.ENTRY_PUBLISHED, new SyncResultCallBack() {
@Override
public void onCompletion(SyncStack syncStack, Error error) {
if (error == null) {
// Called once per page
if (syncStack.getItems() != null) {
Log.d("Contentstack", "Items in this page: " + syncStack.getItems().size());
}
if (syncStack.getSyncToken() != null) {
// Last page: store this token for the next syncToken() call
Log.d("Contentstack", "Sync token: " + syncStack.getSyncToken());
}
} 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());
}Deleted content: sync the assets deleted from the environment
import android.util.Log;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Stack;
import com.contentstack.sdk.SyncResultCallBack;
import com.contentstack.sdk.SyncStack;
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);
stack.syncPublishType(Stack.PublishType.ASSET_DELETED, new SyncResultCallBack() {
@Override
public void onCompletion(SyncStack syncStack, Error error) {
if (error != null) {
Log.e("Contentstack", error.getErrorMessage());
return;
}
if (syncStack.getItems() != null) {
for (JSONObject item : syncStack.getItems()) {
Log.d("Contentstack", item.toString());
}
}
}
});
} catch (Exception e) {
Log.e("Contentstack", e.getMessage());
}syncToken
syncToken fetches the content published, unpublished, or deleted since your last complete sync.
| Name | Type | Description |
|---|---|---|
| syncToken (required) | String | Sync token from the last page of the previous sync. |
| syncCallBack (required) | SyncResultCallBack | Receives each page of the sync. |
Returns: void. The result arrives in the callback.
Validation
- There is no client-side validation. syncToken sends the token as given.
- A null token sends the request without a sync_token parameter.
- An invalid token, an API rejection, or a network failure arrives as a non-null Error in the callback. syncToken does not throw for it.
Additional Resource Refer to Content Delivery API Errors for the full error code list.
Behavior
- syncToken sends a Subsequent Sync request, GET /stacks/sync, with sync_token.
- Read the token from the getSyncToken method of SyncStack on the last page of a sync, and store it until the next run.
- The API returns only the changes since the sync that issued the token. When they hold more than 100 records, the SDK requests every page for you.
- onCompletion(SyncStack syncStack, Error error) fires once per page. On success error is null and syncStack contains that page. On failure syncStack is null.
- The last page contains a new sync token. Store it in place of the old one for the next syncToken call.
Example
Basic usage: fetch the changes since the last sync
import android.util.Log;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Stack;
import com.contentstack.sdk.SyncResultCallBack;
import com.contentstack.sdk.SyncStack;
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);
stack.syncToken("<SYNC_TOKEN>", new SyncResultCallBack() {
@Override
public void onCompletion(SyncStack syncStack, Error error) {
if (error == null) {
// Called once per page
if (syncStack.getItems() != null) {
Log.d("Contentstack", "Changed items in this page: " + syncStack.getItems().size());
}
if (syncStack.getSyncToken() != null) {
// Last page: replace the stored token with this one
Log.d("Contentstack", "New sync token: " + syncStack.getSyncToken());
}
} 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());
}taxonomy
taxonomy creates a Taxonomy object, so you can find the entries tagged with taxonomy terms.
Returns: Taxonomy. A new taxonomy object.
Validation
- There is no client-side validation. taxonomy creates the object and sends no request.
Behavior
- Client-side only. taxonomy makes no request.
- The taxonomy object reads the header map of the stack, so headers you set on the stack apply to its requests.
- Each call returns a new object with no conditions. Chain conditions such as in or below on it, then call find. The Taxonomy class page lists every method.
- Call find from a background thread, as the example below does. find sends its request on the calling thread, so on the main thread Android throws NetworkOnMainThreadException. See the Taxonomy class page.
Example
Basic usage: find the entries tagged with a term
import android.util.Log;
import com.contentstack.sdk.Config;
import com.contentstack.sdk.Contentstack;
import com.contentstack.sdk.Error;
import com.contentstack.sdk.Stack;
import com.contentstack.sdk.Taxonomy;
import com.contentstack.sdk.TaxonomyCallback;
import org.json.JSONObject;
import java.util.Arrays;
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);
Taxonomy taxonomy = stack.taxonomy();
List<String> termUids = Arrays.asList("<TERM_UID>");
new Thread(() -> {
try {
taxonomy.in("taxonomies.<TAXONOMY_UID>", termUids).find(new TaxonomyCallback() {
@Override
public void onResponse(JSONObject response, Error error) {
if (error == null) {
Log.d("Contentstack", response.toString());
} else {
// The API rejected the request
Log.e("Contentstack", "Request failed: " + error.getErrorCode() + " "
+ error.getErrorMessage());
}
}
});
} catch (RuntimeException e) {
// find throws on a network failure instead of calling the callback
Log.e("Contentstack", "Network failure: " + e.getMessage());
}
}).start();
} catch (Exception e) {
// Contentstack.stack throws when a credential is null
Log.e("Contentstack", "Setup failed: " + e.getMessage());
}