Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Android™) Shippo Adding MetadataDemonstrates how to add metadata to the tracking request through a POST request. For more information, see https://goshippo.com/docs/tracking/
// 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); // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. CkHttp http = new CkHttp(); boolean success; // Implements the following CURL command: // curl https://api.goshippo.com/tracks/ \ // -H "Authorization: ShippoToken <API_TOKEN>" \ // -d carrier="shippo" \ // -d tracking_number="SHIPPO_TRANSIT" \ // -d metadata="Order 000123" CkHttpRequest req = new CkHttpRequest(); req.put_HttpVerb("POST"); req.put_Path("/tracks/"); req.put_ContentType("application/x-www-form-urlencoded"); req.AddParam("carrier","shippo"); req.AddParam("tracking_number","SHIPPO_TRANSIT"); req.AddParam("metadata","Order 000123"); req.AddHeader("Authorization","ShippoToken <API_TOKEN>"); CkHttpResponse resp = http.PostUrlEncoded("https://api.goshippo.com/tracks/",req); if (http.get_LastMethodSuccess() == 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) // { // "messages": [ // ], // "carrier": "shippo", // "tracking_number": "SHIPPO_TRANSIT", // "address_from": { // "city": "San Francisco", // "state": "CA", // "zip": "94103", // "country": "US" // }, // "address_to": { // "city": "Chicago", // "state": "IL", // "zip": "60611", // "country": "US" // }, // "eta": "2019-07-07T17:07:44.989Z", // "original_eta": "2019-07-07T17:07:44.989Z", // "servicelevel": { // "token": "shippo_priority", // "name": "Priority Mail" // }, // "metadata": "Shippo test tracking", // "tracking_status": { // "object_created": "2019-07-04T17:07:45.003Z", // "object_updated": null, // "object_id": "ee35fb56f5d04021b36168abedc04573", // "status": "TRANSIT", // "status_details": "Your shipment has departed from the origin.", // "status_date": "2019-07-03T15:02:45.003Z", // "substatus": null, // "location": { // "city": "San Francisco", // "state": "CA", // "zip": "94103", // "country": "US" // } // }, // "tracking_history": [ // { // "object_created": "2019-07-04T17:07:45.005Z", // "object_updated": null, // "object_id": "2121a59f53ed42e0ae0436f636179156", // "status": "UNKNOWN", // "status_details": "The carrier has received the electronic shipment information.", // "status_date": "2019-07-02T12:57:45.005Z", // "substatus": null, // "location": { // "city": "San Francisco", // "state": "CA", // "zip": "94103", // "country": "US" // } // }, // { // "object_created": "2019-07-04T17:07:45.005Z", // "object_updated": null, // "object_id": "06f949db1a8245beaa28df264b368a76", // "status": "TRANSIT", // "status_details": "Your shipment has departed from the origin.", // "status_date": "2019-07-03T15:02:45.005Z", // "substatus": null, // "location": { // "city": "San Francisco", // "state": "CA", // "zip": "94103", // "country": "US" // } // } // ], // "transaction": null, // "test": true // } // 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 object_created; String object_updated; String object_id; String status; String status_details; String status_date; String substatus; String locationCity; String locationState; String locationZip; String locationCountry; String carrier = jResp.stringOf("carrier"); String tracking_number = jResp.stringOf("tracking_number"); 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_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 eta = jResp.stringOf("eta"); String original_eta = jResp.stringOf("original_eta"); String servicelevelToken = jResp.stringOf("servicelevel.token"); String servicelevelName = jResp.stringOf("servicelevel.name"); String metadata = jResp.stringOf("metadata"); String tracking_statusObject_created = jResp.stringOf("tracking_status.object_created"); String tracking_statusObject_updated = jResp.stringOf("tracking_status.object_updated"); String tracking_statusObject_id = jResp.stringOf("tracking_status.object_id"); String tracking_statusStatus = jResp.stringOf("tracking_status.status"); String tracking_statusStatus_details = jResp.stringOf("tracking_status.status_details"); String tracking_statusStatus_date = jResp.stringOf("tracking_status.status_date"); String tracking_statusSubstatus = jResp.stringOf("tracking_status.substatus"); String tracking_statusLocationCity = jResp.stringOf("tracking_status.location.city"); String tracking_statusLocationState = jResp.stringOf("tracking_status.location.state"); String tracking_statusLocationZip = jResp.stringOf("tracking_status.location.zip"); String tracking_statusLocationCountry = jResp.stringOf("tracking_status.location.country"); String transaction = jResp.stringOf("transaction"); boolean test = jResp.BoolOf("test"); int i = 0; int count_i = jResp.SizeOfArray("messages"); while (i < count_i) { jResp.put_I(i); i = i + 1; } i = 0; count_i = jResp.SizeOfArray("tracking_history"); while (i < count_i) { jResp.put_I(i); object_created = jResp.stringOf("tracking_history[i].object_created"); object_updated = jResp.stringOf("tracking_history[i].object_updated"); object_id = jResp.stringOf("tracking_history[i].object_id"); status = jResp.stringOf("tracking_history[i].status"); status_details = jResp.stringOf("tracking_history[i].status_details"); status_date = jResp.stringOf("tracking_history[i].status_date"); substatus = jResp.stringOf("tracking_history[i].substatus"); locationCity = jResp.stringOf("tracking_history[i].location.city"); locationState = jResp.stringOf("tracking_history[i].location.state"); locationZip = jResp.stringOf("tracking_history[i].location.zip"); locationCountry = jResp.stringOf("tracking_history[i].location.country"); 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." } } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.