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

SendGrid: Report daily requests, deliveries, bounces, and blocks

See more SendGrid Examples

Retrieve seven completed UTC dates and print one row per date. The counts are account-level aggregates; a day’s delivered count does not identify which messages sent that day were delivered.

Global email statistics

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.
    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();

    CkDateTime day = new CkDateTime();
    day.SetFromCurrentSystemTime();
    day.AddDays(-1);
    http.SetUrlVar("end",day.getAsIso8601("YYYY-MM-DD",false));
    day.AddDays(-6);
    http.SetUrlVar("start",day.getAsIso8601("YYYY-MM-DD",false));

    success = http.HttpNoBody("GET","https://api.sendgrid.com/v3/stats?start_date={$start}&end_date={$end}&aggregated_by=day",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 days = new CkJsonArray();
    success = days.Load(resp.bodyStr());
    if (success != true) {
        Log.i(TAG, "Unexpected response: expected JSON.");
        return;
        }

    Log.i(TAG, "Date,Requests,Delivered,Bounces,Blocks");
    CkJsonObject row = new CkJsonObject();
    int i = 0;
    while ((i < days.get_Size())) {
        days.ObjectAt2(i,row);
        Log.i(TAG, row.stringOf("date") + "," + String.valueOf(row.IntOf("stats[0].metrics.requests")) + "," + String.valueOf(row.IntOf("stats[0].metrics.delivered"))
             + "," + String.valueOf(row.IntOf("stats[0].metrics.bounces")) + "," + String.valueOf(row.IntOf("stats[0].metrics.blocks")));
        i = i + 1;
        }


  }

  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."
  }
}