Sample code for 30+ languages & platforms
Android™

Shippo Create the Shipment, Get Rates, and Purchase Label

See more Shippo Examples

Demonstrates how retrieve rates and create labels for international shipments.

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

    // Implements the following CURL command:

    // curl https://api.goshippo.com/customs/declarations/ \
    //     -H "Authorization: ShippoToken <API_TOKEN>" \
    //     -H "Content-Type: application/json"  \
    //     -d '{          
    //           "contents_type": "MERCHANDISE",
    //           "non_delivery_option": "RETURN",
    //           "certify": true,
    //           "certify_signer": "Simon Kreuz",
    //           "incoterm": "DDU",
    //           "items": [{
    //                     "description": "T-shirt",
    //                     "quantity": 20,
    //                     "net_weight": "5",
    //                     "mass_unit": "lb",
    //                     "value_amount": "200",
    //                     "value_currency": "USD",
    //                     "tariff_number": "",
    //                     "origin_country": "US"
    //             }]
    //     }'

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

    // The following JSON is sent in the request body.

    // {
    //   "contents_type": "MERCHANDISE",
    //   "non_delivery_option": "RETURN",
    //   "certify": true,
    //   "certify_signer": "Simon Kreuz",
    //   "incoterm": "DDU",
    //   "items": [
    //     {
    //       "description": "T-shirt",
    //       "quantity": 20,
    //       "net_weight": "5",
    //       "mass_unit": "lb",
    //       "value_amount": "200",
    //       "value_currency": "USD",
    //       "tariff_number": "",
    //       "origin_country": "US"
    //     }
    //   ]
    // }

    CkJsonObject json = new CkJsonObject();
    json.UpdateString("contents_type","MERCHANDISE");
    json.UpdateString("non_delivery_option","RETURN");
    json.UpdateBool("certify",true);
    json.UpdateString("certify_signer","Simon Kreuz");
    json.UpdateString("incoterm","DDU");
    json.UpdateString("items[0].description","T-shirt");
    json.UpdateInt("items[0].quantity",20);
    json.UpdateString("items[0].net_weight","5");
    json.UpdateString("items[0].mass_unit","lb");
    json.UpdateString("items[0].value_amount","200");
    json.UpdateString("items[0].value_currency","USD");
    json.UpdateString("items[0].tariff_number","");
    json.UpdateString("items[0].origin_country","US");

    http.SetRequestHeader("Authorization","ShippoToken <API_TOKEN>");
    http.SetRequestHeader("Content-Type","application/json");

    CkHttpResponse resp = new CkHttpResponse();
    success = http.HttpJson("POST","https://api.goshippo.com/customs/declarations/",json,"application/json",resp);
    if (success == false) {
        Log.i(TAG, http.lastErrorText());
        return;
        }

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

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

    int respStatusCode = resp.get_StatusCode();
    Log.i(TAG, "Response Status Code = " + String.valueOf(respStatusCode));
    if (respStatusCode >= 400) {
        Log.i(TAG, "Response Header:");
        Log.i(TAG, resp.header());
        Log.i(TAG, "Failed.");
        return;
        }

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // {
    //   "object_created": "2014-07-17T00:04:06.163Z",
    //   "object_updated": "2014-07-17T00:04:06.163Z",
    //   "object_id": "89436997a794439ab47999701e60392e",
    //   "object_owner": "shippotle@goshippo.com",
    //   "status": "SUCCESS",
    //   "address_from": {
    //     "object_id": "0943ae4e373e4120a99c337e496dcce8",
    //     "validation_results": {},
    //     "is_complete": true,
    //     "company": "",
    //     "street_no": "",
    //     "name": "Mr. Hippo",
    //     "street1": "215 Clayton St.",
    //     "street2": "",
    //     "city": "San Francisco",
    //     "state": "CA",
    //     "zip": "94117",
    //     "country": "US",
    //     "phone": "+15553419393",
    //     "email": "support@goshippo.com",
    //     "is_residential": null
    //   },
    //   "address_to": {
    //     "object_id": "4c7185d353764d0985a6a7825aed8ffb",
    //     "validation_results": {},
    //     "is_complete": true,
    //     "name": "Mrs. Hippo",
    //     "street1": "200 University Ave W",
    //     "city": "Waterloo",
    //     "state": "ON",
    //     "zip": "N2L 3G1",
    //     "country": "CA",
    //     "phone": "+1 555 341 9393",
    //     "email": "support@goshippo.com",
    //     "is_residential": false
    //   },
    //   "address_return": null,
    //   "parcels": [
    //     {
    //       "object_id": "ec952343dd4843c39b42aca620471fd5",
    //       "object_created": "2013-12-01T06:24:21.121Z",
    //       "object_updated": "2013-12-01T06:24:21.121Z",
    //       "object_owner": "shippotle@goshippo.com",
    //       "template": null,
    //       "length": "5",
    //       "width": "5",
    //       "height": "5",
    //       "distance_unit": "in",
    //       "weight": "2",
    //       "mass_unit": "lb",
    //       "value_amount": null,
    //       "value_currency": null,
    //       "metadata": "",
    //       "line_items": [
    //       ],
    //       "test": true
    //     }
    //   ],
    //   "shipment_date": "2013-12-03T12:00:00Z",
    //   "extra": {
    //     "insurance": {
    //       "currency": "",
    //       "amount": ""
    //     },
    //     "reference_1": "",
    //     "reference_2": ""
    //   },
    //   "customs_declaration": "b741b99f95e841639b54272834bc478c",
    //   "rates": [
    //     {
    //       "object_created": "2014-07-17T00:04:06.263Z",
    //       "object_id": "545ab0a1a6ea4c9f9adb2512a57d6d8b",
    //       "object_owner": "shippotle@goshippo.com",
    //       "shipment": "89436997a794439ab47999701e60392e",
    //       "attributes": [
    //       ],
    //       "amount": "5.50",
    //       "currency": "USD",
    //       "amount_local": "5.50",
    //       "currency_local": "USD",
    //       "provider": "USPS",
    //       "provider_image_75": "https://cdn2.goshippo.com/providers/75/USPS.png",
    //       "provider_image_200": "https://cdn2.goshippo.com/providers/200/USPS.png",
    //       "servicelevel": {
    //         "name": "Priority Mail",
    //         "token": "usps_priority",
    //         "terms": ""
    //       },
    //       "days": 2,
    //       "arrives_by": null,
    //       "duration_terms": "Delivery in 1 to 3 business days.",
    //       "messages": [
    //       ],
    //       "carrier_account": "078870331023437cb917f5187429b093",
    //       "test": false,
    //       "zone": 1
    //     },
    //     ...
    //   ],
    //   "carrier_accounts": [
    //   ],
    //   "messages": [
    //   ],
    //   "metadata": "Customer ID 123456"
    // }

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

    String template;
    String length;
    String width;
    String height;
    String distance_unit;
    String weight;
    String mass_unit;
    String value_amount;
    String value_currency;
    boolean test;
    int j;
    int count_j;
    int intVal;

    String object_created = jResp.stringOf("object_created");
    String object_updated = jResp.stringOf("object_updated");
    String object_id = jResp.stringOf("object_id");
    String object_owner = jResp.stringOf("object_owner");
    String status = jResp.stringOf("status");
    String address_fromObject_id = jResp.stringOf("address_from.object_id");
    boolean address_fromIs_complete = jResp.BoolOf("address_from.is_complete");
    String address_fromCompany = jResp.stringOf("address_from.company");
    String address_fromStreet_no = jResp.stringOf("address_from.street_no");
    String address_fromName = jResp.stringOf("address_from.name");
    String address_fromStreet1 = jResp.stringOf("address_from.street1");
    String address_fromStreet2 = jResp.stringOf("address_from.street2");
    String address_fromCity = jResp.stringOf("address_from.city");
    String address_fromState = jResp.stringOf("address_from.state");
    String address_fromZip = jResp.stringOf("address_from.zip");
    String address_fromCountry = jResp.stringOf("address_from.country");
    String address_fromPhone = jResp.stringOf("address_from.phone");
    String address_fromEmail = jResp.stringOf("address_from.email");
    String address_fromIs_residential = jResp.stringOf("address_from.is_residential");
    String address_toObject_id = jResp.stringOf("address_to.object_id");
    boolean address_toIs_complete = jResp.BoolOf("address_to.is_complete");
    String address_toName = jResp.stringOf("address_to.name");
    String address_toStreet1 = jResp.stringOf("address_to.street1");
    String address_toCity = jResp.stringOf("address_to.city");
    String address_toState = jResp.stringOf("address_to.state");
    String address_toZip = jResp.stringOf("address_to.zip");
    String address_toCountry = jResp.stringOf("address_to.country");
    String address_toPhone = jResp.stringOf("address_to.phone");
    String address_toEmail = jResp.stringOf("address_to.email");
    boolean address_toIs_residential = jResp.BoolOf("address_to.is_residential");
    String address_return = jResp.stringOf("address_return");
    String shipment_date = jResp.stringOf("shipment_date");
    String extraInsuranceCurrency = jResp.stringOf("extra.insurance.currency");
    String extraInsuranceAmount = jResp.stringOf("extra.insurance.amount");
    String extraReference_1 = jResp.stringOf("extra.reference_1");
    String extraReference_2 = jResp.stringOf("extra.reference_2");
    String customs_declaration = jResp.stringOf("customs_declaration");
    String metadata = jResp.stringOf("metadata");
    int i = 0;
    int count_i = jResp.SizeOfArray("parcels");
    while (i < count_i) {
        jResp.put_I(i);
        object_id = jResp.stringOf("parcels[i].object_id");
        object_created = jResp.stringOf("parcels[i].object_created");
        object_updated = jResp.stringOf("parcels[i].object_updated");
        object_owner = jResp.stringOf("parcels[i].object_owner");
        template = jResp.stringOf("parcels[i].template");
        length = jResp.stringOf("parcels[i].length");
        width = jResp.stringOf("parcels[i].width");
        height = jResp.stringOf("parcels[i].height");
        distance_unit = jResp.stringOf("parcels[i].distance_unit");
        weight = jResp.stringOf("parcels[i].weight");
        mass_unit = jResp.stringOf("parcels[i].mass_unit");
        value_amount = jResp.stringOf("parcels[i].value_amount");
        value_currency = jResp.stringOf("parcels[i].value_currency");
        metadata = jResp.stringOf("parcels[i].metadata");
        test = jResp.BoolOf("parcels[i].test");
        j = 0;
        count_j = jResp.SizeOfArray("parcels[i].line_items");
        while (j < count_j) {
            jResp.put_J(j);
            j = j + 1;
            }

        i = i + 1;
        }

    i = 0;
    count_i = jResp.SizeOfArray("rates");
    while (i < count_i) {
        jResp.put_I(i);
        intVal = jResp.IntOf("rates[i]");
        j = 0;
        count_j = jResp.SizeOfArray("rates[i].attributes");
        while (j < count_j) {
            jResp.put_J(j);
            j = j + 1;
            }

        j = 0;
        count_j = jResp.SizeOfArray("rates[i].messages");
        while (j < count_j) {
            jResp.put_J(j);
            j = j + 1;
            }

        i = i + 1;
        }

    i = 0;
    count_i = jResp.SizeOfArray("carrier_accounts");
    while (i < count_i) {
        jResp.put_I(i);
        i = i + 1;
        }

    i = 0;
    count_i = jResp.SizeOfArray("messages");
    while (i < count_i) {
        jResp.put_I(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."
  }
}