Android™ Requires Chilkat v11.0.0+
Android™
SendGrid: Save and verify a topic opt-out
See more SendGrid Examples
Use this when a signed-in reader turns off Product updates. Look up the configured group before submitting: SendGrid warns that a nonexistent or deleted group can cause a global suppression instead. Keep the group active while processing preferences. Deleting a marketing contact is not a substitute for recording an opt-out.
Add group suppressions ยท Check specific addresses
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();
// Take the address from the authenticated account and the group ID from configuration.
String address = "reader@example.com";
http.SetUrlVar("group","12345");
success = http.HttpNoBody("GET","https://api.sendgrid.com/v3/asm/groups/{$group}",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 groupInfo = new CkJsonObject();
success = groupInfo.Load(resp.bodyStr());
if (success != true) {
Log.i(TAG, "Unexpected response: expected JSON.");
return;
}
if (groupInfo.StringOfEquals("name","Product updates",true) != true) {
Log.i(TAG, "Unexpected unsubscribe group; no preference was changed.");
return;
}
CkJsonObject addresses = new CkJsonObject();
addresses.UpdateString("recipient_emails[0]",address);
success = http.HttpJson("POST","https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions",addresses,"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() != 201) {
Log.i(TAG, "HTTP " + String.valueOf(resp.get_StatusCode()) + ": " + resp.bodyStr());
return;
}
success = http.HttpJson("POST","https://api.sendgrid.com/v3/asm/groups/{$group}/suppressions/search",addresses,"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() != 200) {
Log.i(TAG, "HTTP " + String.valueOf(resp.get_StatusCode()) + ": " + resp.bodyStr());
return;
}
CkJsonArray matches = new CkJsonArray();
success = matches.Load(resp.bodyStr());
if (success != true) {
Log.i(TAG, "Unexpected response: expected JSON.");
return;
}
if (matches.get_Size() == 1) {
Log.i(TAG, "Confirmed opt-out: " + matches.stringAt(0));
}
else {
Log.i(TAG, "Opt-out not confirmed. Recheck this group.");
}
}
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."
}
}