Android™ Requires Chilkat v11.0.0+
Android™
SendGrid: Add or update list contacts and check the import job
See more SendGrid Examples
Use a Marketing Campaigns API key with contact access and the ID of an existing list. A 202 response queues the upsert; it does not confirm that the contacts were saved. Save the job ID and use it to recheck a pending import without submitting the contacts again. The error report may contain contact data; handle it privately.
Add or update contacts ยท Import job status
Chilkat Android™ Downloads
// 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.
success = false;
CkHttp http = new CkHttp();
http.put_RequireSslCertVerify(true);
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.put_AuthToken("SENDGRID_API_KEY");
CkHttpResponse resp = new CkHttpResponse();
String jobId = "";
if (jobId == "") {
CkJsonObject contacts = new CkJsonObject();
contacts.UpdateString("list_ids[0]","YOUR_LIST_UUID");
contacts.UpdateString("contacts[0].email","avery@example.com");
contacts.UpdateString("contacts[0].first_name","Avery");
contacts.UpdateString("contacts[1].email","robin@example.com");
contacts.UpdateString("contacts[1].first_name","Robin");
success = http.HttpJson("PUT","https://api.sendgrid.com/v3/marketing/contacts",contacts,"application/json",resp);
if (success != true) {
if (resp.get_StatusCode() == 0) {
Log.i(TAG, http.lastErrorText());
}
else {
Log.i(TAG, "HTTP " + String.valueOf(resp.get_StatusCode()) + ": " + resp.bodyStr());
}
return;
}
if (resp.get_StatusCode() != 202) {
Log.i(TAG, "HTTP " + String.valueOf(resp.get_StatusCode()) + ": " + resp.bodyStr());
return;
}
CkJsonObject queued = new CkJsonObject();
success = queued.Load(resp.bodyStr());
if (success != true) {
Log.i(TAG, "Unexpected response: expected JSON.");
return;
}
if (queued.HasMember("job_id") != true) {
Log.i(TAG, "No job ID returned.");
return;
}
jobId = queued.stringOf("job_id");
Log.i(TAG, "Save this import job ID: " + jobId);
}
http.SetUrlVar("job",jobId);
success = http.HttpNoBody("GET","https://api.sendgrid.com/v3/marketing/contacts/imports/{$job}",resp);
if (success != true) {
if (resp.get_StatusCode() == 0) {
Log.i(TAG, http.lastErrorText());
}
else {
Log.i(TAG, "HTTP " + String.valueOf(resp.get_StatusCode()) + ": " + resp.bodyStr());
}
return;
}
if (resp.get_StatusCode() != 200) {
Log.i(TAG, "HTTP " + String.valueOf(resp.get_StatusCode()) + ": " + resp.bodyStr());
return;
}
CkJsonObject job = new CkJsonObject();
success = job.Load(resp.bodyStr());
if (success != true) {
Log.i(TAG, "Unexpected response: expected JSON.");
return;
}
String state = job.stringOf("status");
if (state == "pending") {
Log.i(TAG, "Still pending. Check again later with jobId = " + jobId);
return;
}
Log.i(TAG, "Job status: " + state);
Log.i(TAG, "Created: " + String.valueOf(job.IntOf("results.created_count")) + ", updated: " + String.valueOf(job.IntOf("results.updated_count")) + ", errors: "
+ String.valueOf(job.IntOf("results.errored_count")));
if ((state == "errored") or (state == "failed")) {
Log.i(TAG, "Review the job before retrying. If present, results.errors_url identifies the private error report.");
}
}
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."
}
}