Sample code for 30+ languages & platforms
Android™

Google People API - List the User's Contacts

See more Google People Examples

Gets a list of people in the user's contacts.

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;

    //  This example assumes the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    //  It is assumed we previously obtained an OAuth2 access token.
    //  This example loads the JSON access token file 

    CkJsonObject jsonToken = new CkJsonObject();
    success = jsonToken.LoadFile("qa_data/tokens/googlePeople.json");
    if (success != true) {
        Log.i(TAG, "Failed to load googleContacts.json");
        return;
        }

    CkHttp http = new CkHttp();

    http.put_AuthToken(jsonToken.stringOf("access_token"));

    CkStringBuilder sbResponseBody = new CkStringBuilder();
    success = http.QuickGetSb("https://people.googleapis.com/v1/people/me/connections?personFields=names,addresses,emailAddresses",sbResponseBody);
    if (success == false) {
        Log.i(TAG, http.lastErrorText());
        Log.i(TAG, sbResponseBody.getAsString());
        return;
        }

    //  Sample JSON Response

    //  Use this online tool to generate parsing code from sample JSON: 
    //  Generate Parsing Code from JSON

    //  {
    //    "connections": [
    //      {
    //        "resourceName": "people/c8871101035606120608",
    //        "etag": "%EgkBAj0JQhBANy4aBAECBQciDERLSGdVTFNPbzNJPQ==",
    //        "names": [
    //          {
    //            "metadata": {
    //              "primary": true,
    //              "source": {
    //                "type": "CONTACT",
    //                "id": "7b1c7b6409e718a0"
    //              }
    //            },
    //            "displayName": "Chilkat Cloud",
    //            "familyName": "Cloud",
    //            "givenName": "Chilkat",
    //            "displayNameLastFirst": "Cloud, Chilkat",
    //            "unstructuredName": "Chilkat Cloud"
    //          }
    //        ],
    //        "emailAddresses": [
    //          {
    //            "metadata": {
    //              "primary": true,
    //              "source": {
    //                "type": "CONTACT",
    //                "id": "7b1c7b6409e718a0"
    //              }
    //            },
    //            "value": "support@***.com"
    //          }
    //        ]
    //      },
    //      {
    //        "resourceName": "people/c7607335470312011517",
    //        "etag": "%EgkBAj0JQhBANy4aBAECBQciDGZETUtHVTVMazI4PQ==",
    //        "names": [
    //          {
    //            "metadata": {
    //              "primary": true,
    //              "source": {
    //                "type": "CONTACT",
    //                "id": "6992af4e0b2d36fd"
    //              }
    //            },
    //            "displayName": "Matt Smith",
    //            "familyName": "Smith",
    //            "givenName": "Matt",
    //            "displayNameLastFirst": "Smith, Matt",
    //            "unstructuredName": "Matt Smith"
    //          }
    //        ],
    //        "addresses": [
    //          {
    //            "metadata": {
    //              "primary": true,
    //              "source": {
    //                "type": "CONTACT",
    //                "id": "6992af4e0b2d36fd"
    //              }
    //            },
    //            "formattedValue": "2222 E Foorest Ave\nWheaton, IL 60999\nUS",
    //            "streetAddress": "2222 E Foorest Ave",
    //            "city": "Wheaton",
    //            "region": "IL",
    //            "postalCode": "60999",
    //            "country": "US",
    //            "countryCode": "US"
    //          }
    //        ],
    //        "emailAddresses": [
    //          {
    //            "metadata": {
    //              "primary": true,
    //              "source": {
    //                "type": "CONTACT",
    //                "id": "6992af4e0b2d36fd"
    //              }
    //            },
    //            "value": "matt@***.com"
    //          },
    //          {
    //            "metadata": {
    //              "source": {
    //                "type": "CONTACT",
    //                "id": "6992af4e0b2d36fd"
    //              }
    //            },
    //            "value": "admin@***.com"
    //          }
    //        ]
    //      }
    //    ],
    //    "totalPeople": 2,
    //    "totalItems": 2
    //  }

    CkJsonObject json = new CkJsonObject();
    json.put_EmitCompact(false);
    json.Load(sbResponseBody.getAsString());
    Log.i(TAG, json.emit());

    String resourceName;
    String etag;
    int j;
    int count_j;
    boolean metadataPrimary;
    String metadataSourceType;
    String metadataSourceId;
    String displayName;
    String familyName;
    String givenName;
    String displayNameLastFirst;
    String unstructuredName;
    String value;
    String formattedValue;
    String streetAddress;
    String city;
    String region;
    String postalCode;
    String country;
    String countryCode;

    int totalPeople = json.IntOf("totalPeople");
    int totalItems = json.IntOf("totalItems");
    int i = 0;
    int count_i = json.SizeOfArray("connections");
    while (i < count_i) {
        json.put_I(i);
        resourceName = json.stringOf("connections[i].resourceName");
        etag = json.stringOf("connections[i].etag");
        j = 0;
        count_j = json.SizeOfArray("connections[i].names");
        while (j < count_j) {
            json.put_J(j);
            metadataPrimary = json.BoolOf("connections[i].names[j].metadata.primary");
            metadataSourceType = json.stringOf("connections[i].names[j].metadata.source.type");
            metadataSourceId = json.stringOf("connections[i].names[j].metadata.source.id");
            displayName = json.stringOf("connections[i].names[j].displayName");
            familyName = json.stringOf("connections[i].names[j].familyName");
            givenName = json.stringOf("connections[i].names[j].givenName");
            displayNameLastFirst = json.stringOf("connections[i].names[j].displayNameLastFirst");
            unstructuredName = json.stringOf("connections[i].names[j].unstructuredName");
            j = j + 1;
            }

        j = 0;
        count_j = json.SizeOfArray("connections[i].emailAddresses");
        while (j < count_j) {
            json.put_J(j);
            metadataPrimary = json.BoolOf("connections[i].emailAddresses[j].metadata.primary");
            metadataSourceType = json.stringOf("connections[i].emailAddresses[j].metadata.source.type");
            metadataSourceId = json.stringOf("connections[i].emailAddresses[j].metadata.source.id");
            value = json.stringOf("connections[i].emailAddresses[j].value");
            j = j + 1;
            }

        j = 0;
        count_j = json.SizeOfArray("connections[i].addresses");
        while (j < count_j) {
            json.put_J(j);
            metadataPrimary = json.BoolOf("connections[i].addresses[j].metadata.primary");
            metadataSourceType = json.stringOf("connections[i].addresses[j].metadata.source.type");
            metadataSourceId = json.stringOf("connections[i].addresses[j].metadata.source.id");
            formattedValue = json.stringOf("connections[i].addresses[j].formattedValue");
            streetAddress = json.stringOf("connections[i].addresses[j].streetAddress");
            city = json.stringOf("connections[i].addresses[j].city");
            region = json.stringOf("connections[i].addresses[j].region");
            postalCode = json.stringOf("connections[i].addresses[j].postalCode");
            country = json.stringOf("connections[i].addresses[j].country");
            countryCode = json.stringOf("connections[i].addresses[j].countryCode");
            j = j + 1;
            }

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