Sample code for 30+ languages & platforms
Android™

GetHarvest - Test Personal Access Token

See more GetHarvest Examples

Demonstrates how to test your GetHarvest Personal Access Token

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.

    CkHttp http = new CkHttp();

    http.SetRequestHeader("User-Agent","Harvest API Example");
    http.SetRequestHeader("Harvest-Account-ID","ACCOUNT_ID");
    http.SetRequestHeader("Authorization","Bearer ACCESS_TOKEN");

    CkStringBuilder sbResponseBody = new CkStringBuilder();
    success = http.QuickGetSb("https://api.harvestapp.com/api/v2/users/me.json",sbResponseBody);
    if (success != true) {
        Log.i(TAG, http.lastErrorText());
        return;
        }

    CkJsonObject jResp = new CkJsonObject();
    jResp.LoadSb(sbResponseBody);
    jResp.put_EmitCompact(false);

    Log.i(TAG, "Response Body:");
    Log.i(TAG, jResp.emit());

    int respStatusCode = http.get_LastStatus();
    Log.i(TAG, "Response Status Code = " + String.valueOf(respStatusCode));

    if (respStatusCode >= 400) {
        Log.i(TAG, "Response Header:");
        Log.i(TAG, http.lastResponseHeader());
        return;
        }

    // Sample JSON response:

    // {
    //   "id": 2826142,
    //   "first_name": "Joe",
    //   "last_name": "Johnson",
    //   "email": "joe@example.com",
    //   "telephone": "",
    //   "timezone": "Central Time (US & Canada)",
    //   "weekly_capacity": 126000,
    //   "has_access_to_all_future_projects": false,
    //   "is_contractor": false,
    //   "is_admin": true,
    //   "is_project_manager": false,
    //   "can_see_rates": true,
    //   "can_create_projects": true,
    //   "can_create_invoices": true,
    //   "is_active": true,
    //   "created_at": "2019-06-06T15:57:58Z",
    //   "updated_at": "2019-06-06T15:58:32Z",
    //   "default_hourly_rate": 175.0,
    //   "cost_rate": 60.0,
    //   "roles": [
    //     "role1",
    //     "role2"
    //   ],
    //   "avatar_url": "https://d3s3969qhosaug.cloudfront.net/default-avatars/4d46.png?1559836678"
    // }

    // Sample code for parsing the JSON response...
    // Use the following online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    int id;
    String first_name;
    String last_name;
    String email;
    String telephone;
    String timezone;
    int weekly_capacity;
    boolean has_access_to_all_future_projects;
    boolean is_contractor;
    boolean is_admin;
    boolean is_project_manager;
    boolean can_see_rates;
    boolean can_create_projects;
    boolean can_create_invoices;
    boolean is_active;
    String created_at;
    String updated_at;
    String default_hourly_rate;
    String cost_rate;
    String avatar_url;
    int i;
    int count_i;
    String strVal;

    id = jResp.IntOf("id");
    first_name = jResp.stringOf("first_name");
    last_name = jResp.stringOf("last_name");
    email = jResp.stringOf("email");
    telephone = jResp.stringOf("telephone");
    timezone = jResp.stringOf("timezone");
    weekly_capacity = jResp.IntOf("weekly_capacity");
    has_access_to_all_future_projects = jResp.BoolOf("has_access_to_all_future_projects");
    is_contractor = jResp.BoolOf("is_contractor");
    is_admin = jResp.BoolOf("is_admin");
    is_project_manager = jResp.BoolOf("is_project_manager");
    can_see_rates = jResp.BoolOf("can_see_rates");
    can_create_projects = jResp.BoolOf("can_create_projects");
    can_create_invoices = jResp.BoolOf("can_create_invoices");
    is_active = jResp.BoolOf("is_active");
    created_at = jResp.stringOf("created_at");
    updated_at = jResp.stringOf("updated_at");
    default_hourly_rate = jResp.stringOf("default_hourly_rate");
    cost_rate = jResp.stringOf("cost_rate");
    avatar_url = jResp.stringOf("avatar_url");
    i = 0;
    count_i = jResp.SizeOfArray("roles");
    while (i < count_i) {
        jResp.put_I(i);
        strVal = jResp.stringOf("roles[i]");
        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."
  }
}