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
(Java) UPS Tracking APIDemonstrates making a call to the UPS tracking REST API. Parses the tracking response and extracts the base64 signature image to a gif file.
import com.chilkatsoft.*; public class ChilkatExample { static { try { System.loadLibrary("chilkat"); } catch (UnsatisfiedLinkError e) { System.err.println("Native code library failed to load.\n" + e); System.exit(1); } } public static void main(String argv[]) { // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. boolean success; CkHttp http = new CkHttp(); // This is the testing endpoint for the tracking API: String url = "https://wwwcie.ups.com/rest/Track"; // Send an HTTP request with the following JSON body: // { // "UPSSecurity": { // "UsernameToken": { // "Username": "Your Username", // "Password": "Your Password" // }, // "ServiceAccessToken": { // "AccessLicenseNumber": "Your Access License Number" // } // }, // "TrackRequest": { // "Request": { // "RequestOption": "1", // "TransactionReference": { // "CustomerContext": "Your Test Case Summary Description" // } // }, // "InquiryNumber": "YourTrackingNumber" // } // } // // Build the above JSON. CkJsonObject json = new CkJsonObject(); json.UpdateString("UPSSecurity.UsernameToken.Username","UPS_USERNAME"); json.UpdateString("UPSSecurity.UsernameToken.Password","UPS_PASSWORD"); json.UpdateString("UPSSecurity.ServiceAccessToken.AccessLicenseNumber","UPS_ACCESS_KEY"); // Request all activity... json.UpdateString("TrackRequest.Request.RequestOption","activity"); json.UpdateString("TrackRequest.Request.TransactionReference.CustomerContext","Your Test Case Summary Description"); json.UpdateString("TrackRequest.InquiryNumber","1Z12345E0205271688"); CkStringBuilder sb = new CkStringBuilder(); CkHttpResponse resp = http.PostJson3(url,"application/json",json); if (http.get_LastMethodSuccess() != true) { System.out.println(http.lastErrorText()); return; } System.out.println("status = " + resp.get_StatusCode()); // A 200 response status indicate success. if (resp.get_StatusCode() != 200) { System.out.println(resp.bodyStr()); System.out.println("Failed."); return; } json.Load(resp.bodyStr()); json.put_EmitCompact(false); System.out.println(json.emit()); // { // "TrackResponse": { // "Response": { // "ResponseStatus": { // "Code": "1", // "Description": "Success" // }, // "TransactionReference": { // "CustomerContext": "Your Test Case Summary Description" // } // }, // "Shipment": { // "InquiryNumber": { // "Code": "01", // "Description": "ShipmentIdentificationNumber", // "Value": "1Z12345E0205271688" // }, // "ShipmentType": { // "Code": "01", // "Description": "Small Package" // }, // "ShipperNumber": "12345E", // "Service": { // "Code": "002", // "Description": "2ND DAY AIR" // }, // "ReferenceNumber": { // "Code": "01", // "Value": "LINE4AND115" // }, // "PickupDate": "19990608", // "Package": { // "TrackingNumber": "1Z12345E0205271688", // "Activity": [ // { // "ActivityLocation": { // "Address": { // "City": "ANYTOWN", // "StateProvinceCode": "GA", // // "PostalCode": "30340", // "CountryCode": "US" // }, // "Code": "ML", // "Description": "BACK DOOR", // "SignedForByName": "JOHN DOE" // }, // "Status": { // "Type": "D", // "Description": "DELIVERED", // "Code": "KM" // }, // "Date": "19990610", // "Time": "120000", // "Document": { // "Type": { // "Code": "01", // "Description": "Signature Image" // }, // "Content": "R0lGODdhoA ... JU9Y8RdHsRKLMVJ4MVDMREAAADs=", // "Format": { // "Code": "01", // "Description": "GIF" // } // } // }, // { // "Status": { // "Type": "M", // "Description": "BILLING INFORMATION RECEIVED. SHIPMENT DATE PENDING.", // "Code": "MP" // }, // "Date": "19990608", // "Time": "120000" // } // ], // "PackageWeight": { // "UnitOfMeasurement": { // "Code": "LBS" // }, // "Weight": "5.00" // }, // "ReferenceNumber": [ // { // "Code": "01", // "Value": "LINE4AND115" // }, // { // "Code": "08", // "Value": "LJ67Y5" // } // ] // } // }, // "Disclaimer": "You are using UPS tracking service on customer integration test environment, please switch to UPS production environment once you finish the test. The URL is https://onlinetools.ups.com/webservices/Track" // } // } // Use the online tool at Generate JSON Parsing Code // to generate JSON parsing code. String statusCode = json.stringOf("TrackResponse.Response.ResponseStatus.Code"); String statusDescription = json.stringOf("TrackResponse.Response.ResponseStatus.Description"); System.out.println("statusCode: " + statusCode); System.out.println("statusDescription" + statusDescription); String customerContext = json.stringOf("TrackResponse.Response.TransactionReference.CustomerContext"); String inquiryNumberCode = json.stringOf("TrackResponse.Shipment.InquiryNumber.Code"); String inquiryNumberDescription = json.stringOf("TrackResponse.Shipment.InquiryNumber.Description"); String inquiryNumberValue = json.stringOf("TrackResponse.Shipment.InquiryNumber.Value"); String shipmentTypeCode = json.stringOf("TrackResponse.Shipment.ShipmentType.Code"); String shipmentTypeDescription = json.stringOf("TrackResponse.Shipment.ShipmentType.Description"); String shipperNumber = json.stringOf("TrackResponse.Shipment.ShipperNumber"); String serviceCode = json.stringOf("TrackResponse.Shipment.Service.Code"); String serviceDescription = json.stringOf("TrackResponse.Shipment.Service.Description"); String referenceNumberCode = json.stringOf("TrackResponse.Shipment.ReferenceNumber.Code"); String referenceNumberValue = json.stringOf("TrackResponse.Shipment.ReferenceNumber.Value"); String pickupDate = json.stringOf("TrackResponse.Shipment.PickupDate"); String trackingNumber = json.stringOf("TrackResponse.Shipment.Package.TrackingNumber"); String unitOfMeasurementCode = json.stringOf("TrackResponse.Shipment.Package.PackageWeight.UnitOfMeasurement.Code"); String weight = json.stringOf("TrackResponse.Shipment.Package.PackageWeight.Weight"); String disclaimer = json.stringOf("TrackResponse.Disclaimer"); int i = 0; int activityCount = json.SizeOfArray("TrackResponse.Shipment.Package.Activity"); System.out.println("activityCount: " + activityCount); while (i < activityCount) { System.out.println("-- activity " + i); json.put_I(i); if (json.HasMember("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City") == true) { String city = json.stringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City"); System.out.println("city: " + city); String provinceCode = json.stringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.StateProvinceCode"); String postalCode = json.stringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.PostalCode"); String countryCode = json.stringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.CountryCode"); } String locationCode = json.stringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Code"); String locationDescription = json.stringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Description"); String locationSignedForByName = json.stringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.SignedForByName"); String activityStatusType = json.stringOf("TrackResponse.Shipment.Package.Activity[i].Status.Type"); System.out.println("activityStatusType: " + activityStatusType); String activityStatusDescription = json.stringOf("TrackResponse.Shipment.Package.Activity[i].Status.Description"); System.out.println("activityStatusDescription: " + activityStatusDescription); String activityStatusCode = json.stringOf("TrackResponse.Shipment.Package.Activity[i].Status.Code"); System.out.println("activityStatusCode: " + activityStatusCode); String activityDate = json.stringOf("TrackResponse.Shipment.Package.Activity[i].Date"); String activityTime = json.stringOf("TrackResponse.Shipment.Package.Activity[i].Time"); if (json.HasMember("TrackResponse.Shipment.Package.Activity[i].Document") == true) { int typeCode = json.IntOf("TrackResponse.Shipment.Package.Activity[i].Document.Type.Code"); String typeDescription = json.stringOf("TrackResponse.Shipment.Package.Activity[i].Document.Type.Description"); String documentContent = json.stringOf("TrackResponse.Shipment.Package.Activity[i].Document.Content"); String documentFormatCode = json.stringOf("TrackResponse.Shipment.Package.Activity[i].Document.Format.Code"); // Format description would be something like "GIF" for a signature image. String documentFormatDescription = json.stringOf("TrackResponse.Shipment.Package.Activity[i].Document.Format.Description"); // 01 - Signature Image // 02 - Delivery Receipt // 03 - Free Astray // 04 - POD if (typeCode == 1) { // We have a signature image. Get the image data and save to a file. CkStringBuilder sbImagePath = new CkStringBuilder(); sbImagePath.Append("qa_output/sig_"); sbImagePath.Append(trackingNumber); sbImagePath.Append("."); sbImagePath.Append(documentFormatDescription); CkBinData imageData = new CkBinData(); success = imageData.AppendEncoded(documentContent,"base64"); // Write to "qa_output/sig_1Z12345E0205271688.GIF" success = imageData.WriteFile(sbImagePath.getAsString()); } } i = i+1; } i = 0; int refnumCount = json.SizeOfArray("TrackResponse.Shipment.Package.ReferenceNumber"); while (i < refnumCount) { json.put_I(i); String refnumCode = json.stringOf("TrackResponse.Shipment.Package.ReferenceNumber[i].Code"); String refnumValue = json.stringOf("TrackResponse.Shipment.Package.ReferenceNumber[i].Value"); i = i+1; } System.out.println("Success."); } } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.