Sample code for 30+ languages & platforms
Android™ Requires Chilkat v11.0.0+

Constant Contact V3: Find a campaign and its primary email activity

See more Constant Contact Examples

Enter the unique campaign name from the account. Follow the campaign collection’s cursor until the name matches, then fetch that campaign’s details. Select the activity whose role is primary_email. A permalink or resend can appear first.

The example writes the two IDs to newsletter-selection.json, the same file produced by the draft example. Run this lookup when selecting an existing newsletter for review or scheduling.

Authorize the application using the same account and credential store.

Constant Contact developer guide

Chilkat Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = false;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    CkSecrets vault = new CkSecrets();
    vault.put_Location("local_manager");
    vault.put_AutoSplit(true);
    CkJsonObject entry = new CkJsonObject();
    entry.UpdateString("appName","ConstantContactV3");
    entry.UpdateString("service","OAuth2");
    entry.UpdateString("username","my-account");
    CkJsonObject tokens = new CkJsonObject();
    boolean loaded = vault.GetSecretJson(entry,tokens);
    if (loaded != true) {
        Log.i(TAG, "Run the desktop authorization example using this account and store.");
        return;
        }

    if (tokens.JsonTypeOf("access_token") != 1) {
        Log.i(TAG, "The saved authorization has no access token.");
        return;
        }

    CkHttp http = new CkHttp();
    http.put_RequireSslCertVerify(true);
    http.put_AuthToken(tokens.stringOf("access_token"));
    CkHttpResponse resp = new CkHttpResponse();

    String campaignName = "Workshop newsletter";
    String campaignId = "";
    CkStringBuilder url = new CkStringBuilder();
    url.Append("https://api.cc.email/v3/emails?limit=50");
    CkJsonObject page = new CkJsonObject();
    while (((url.get_Length() > 0) and (campaignId == ""))) {
        success = http.HttpNoBody("GET",url.getAsString(),resp);
        if ((success != true) and (resp.get_StatusCode() <= 0)) {
            Log.i(TAG, "Read campaigns: HTTP request failed.");
            return;
            }

        if ((success != true) or (resp.get_StatusCode() != 200)) {
            Log.i(TAG, "Read campaigns: HTTP " + String.valueOf(resp.get_StatusCode()) + " " + resp.bodyStr());
            return;
            }

        success = page.Load(resp.bodyStr());
        int count = page.SizeOfArray("campaigns");
        if ((success != true) or (count < 0)) {
            Log.i(TAG, "Expected a page of campaigns.");
            return;
            }

        int i = 0;
        while (((i < count) and (campaignId == ""))) {
            page.put_I(i);
            if (page.StringOfEquals("campaigns[i].name",campaignName,true) == true) {
                campaignId = page.stringOf("campaigns[i].campaign_id");
                }

            i = i + 1;
            }

        url.Clear();
        if ((campaignId == "") and (page.JsonTypeOf("_links.next.href") == 1)) {
            url.Append(page.stringOf("_links.next.href"));
            if (url.StartsWith("/v3/emails?",true) != true) {
                Log.i(TAG, "Unexpected campaign pagination link.");
                return;
                }

            url.Prepend("https://api.cc.email");
            }

        }

    if (campaignId == "") {
        Log.i(TAG, "No campaign named " + campaignName);
        return;
        }

    http.SetUrlVar("campaign",campaignId);
    success = http.HttpNoBody("GET","https://api.cc.email/v3/emails/{$campaign}",resp);
    if ((success != true) and (resp.get_StatusCode() <= 0)) {
        Log.i(TAG, "Read campaign: HTTP request failed.");
        return;
        }

    if ((success != true) or (resp.get_StatusCode() != 200)) {
        Log.i(TAG, "Read campaign: HTTP " + String.valueOf(resp.get_StatusCode()) + " " + resp.bodyStr());
        return;
        }

    CkJsonObject campaign = new CkJsonObject();
    success = campaign.Load(resp.bodyStr());
    if (success != true) {
        Log.i(TAG, "Expected a JSON object in the response.");
        return;
        }

    //  The campaign identifies the newsletter. The primary activity identifies the email to test or schedule.
    CkJsonObject primary = new CkJsonObject();
    success = campaign.FindRecord2("campaign_activities","role","primary_email",true,primary);
    if ((success != true) or (primary.JsonTypeOf("campaign_activity_id") != 1)) {
        Log.i(TAG, "No primary email activity ID in the response.");
        return;
        }

    String selectedCampaign = campaign.stringOf("campaign_id");
    String selectedActivity = primary.stringOf("campaign_activity_id");
    if ((selectedCampaign == "") or (selectedActivity == "")) {
        Log.i(TAG, "Missing campaign or activity ID.");
        return;
        }

    Log.i(TAG, "Campaign ID: " + selectedCampaign);
    Log.i(TAG, "Primary activity ID: " + selectedActivity);
    CkJsonObject selection = new CkJsonObject();
    selection.UpdateString("name",campaign.stringOf("name"));
    selection.UpdateString("campaign_id",selectedCampaign);
    selection.UpdateString("campaign_activity_id",selectedActivity);
    success = selection.WriteFile("newsletter-selection.json");
    if (success != true) {
        Log.i(TAG, "Could not save newsletter-selection.json. Keep the IDs printed above.");
        return;
        }

    Log.i(TAG, "Saved newsletter-selection.json.");

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}