Java
Chilkat2 Java
GetHarvest - Get Client Contacts
See more GetHarvest Examples
Returns a list of your contacts.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;
public class ChilkatExample {
public static void main(String[] args) {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Http http = new Http();
// Implements the following CURL command:
// curl "https://api.harvestapp.com/v2/contacts" \
// -H "Authorization: Bearer ACCESS_TOKEN" \
// -H "Harvest-Account-Id: ACCOUNT_ID" \
// -H "User-Agent: MyApp (yourname@example.com)"
http.setRequestHeader("User-Agent", "MyApp (yourname@example.com)");
http.setRequestHeader("Authorization", "Bearer ACCESS_TOKEN");
http.setRequestHeader("Harvest-Account-Id", "ACCOUNT_ID");
StringBuilder sbResponseBody = new StringBuilder();
try {
http.quickGetSb("https://api.harvestapp.com/v2/contacts", sbResponseBody);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
JsonObject jResp = new JsonObject();
jResp.loadSb(sbResponseBody);
jResp.setEmitCompact(false);
System.out.println("Response Body:");
System.out.println(jResp.emit());
int respStatusCode = http.getLastStatus();
System.out.println("Response Status Code = " + respStatusCode);
if (respStatusCode >= 400) {
System.out.println("Response Header:");
System.out.println(http.getLastHeader());
System.out.println("Failed.");
return;
}
// Sample JSON response:
// {
// "contacts": [
// {
// "id": 4706479,
// "title": "Owner",
// "first_name": "Jane",
// "last_name": "Doe",
// "email": "janedoe@example.com",
// "phone_office": "(203) 697-8885",
// "phone_mobile": "(203) 697-8886",
// "fax": "(203) 697-8887",
// "created_at": "2017-06-26T21:20:07Z",
// "updated_at": "2017-06-26T21:27:07Z",
// "client": {
// "id": 5735774,
// "name": "ABC Corp"
// }
// },
// {
// "id": 4706453,
// "title": "Manager",
// "first_name": "Richard",
// "last_name": "Roe",
// "email": "richardroe@example.com",
// "phone_office": "(318) 515-5905",
// "phone_mobile": "(318) 515-5906",
// "fax": "(318) 515-5907",
// "created_at": "2017-06-26T21:06:55Z",
// "updated_at": "2017-06-26T21:27:20Z",
// "client": {
// "id": 5735776,
// "name": "123 Industries"
// }
// }
// ],
// "per_page": 100,
// "total_pages": 1,
// "total_entries": 2,
// "next_page": null,
// "previous_page": null,
// "page": 1,
// "links": {
// "first": "https://api.harvestapp.com/v2/contacts?page=1&per_page=100",
// "next": null,
// "previous": null,
// "last": "https://api.harvestapp.com/v2/contacts?page=1&per_page=100"
// }
// }
// 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 = 0;
String title = "";
String first_name = "";
String last_name = "";
String email = "";
String phone_office = "";
String phone_mobile = "";
String fax = "";
String created_at = "";
String updated_at = "";
int clientId = 0;
String clientName = "";
int per_page = jResp.intOf("per_page");
int total_pages = jResp.intOf("total_pages");
int total_entries = jResp.intOf("total_entries");
String next_page = jResp.stringOf("next_page");
String previous_page = jResp.stringOf("previous_page");
int page = jResp.intOf("page");
String linksFirst = jResp.stringOf("links.first");
String linksNext = jResp.stringOf("links.next");
String linksPrevious = jResp.stringOf("links.previous");
String linksLast = jResp.stringOf("links.last");
int i = 0;
int count_i = jResp.sizeOfArray("contacts");
while (i < count_i) {
jResp.setI(i);
id = jResp.intOf("contacts[i].id");
title = jResp.stringOf("contacts[i].title");
first_name = jResp.stringOf("contacts[i].first_name");
last_name = jResp.stringOf("contacts[i].last_name");
email = jResp.stringOf("contacts[i].email");
phone_office = jResp.stringOf("contacts[i].phone_office");
phone_mobile = jResp.stringOf("contacts[i].phone_mobile");
fax = jResp.stringOf("contacts[i].fax");
created_at = jResp.stringOf("contacts[i].created_at");
updated_at = jResp.stringOf("contacts[i].updated_at");
clientId = jResp.intOf("contacts[i].client.id");
clientName = jResp.stringOf("contacts[i].client.name");
i = i + 1;
}
}
}