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
(Objective-C) 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 <CkoHttp.h> #import <NSString.h> #import <CkoJsonObject.h> #import <CkoStringBuilder.h> #import <CkoHttpResponse.h> #import <CkoBinData.h> // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. BOOL success; CkoHttp *http = [[CkoHttp alloc] init]; // This is the testing endpoint for the tracking API: NSString *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. CkoJsonObject *json = [[CkoJsonObject alloc] init]; [json UpdateString: @"UPSSecurity.UsernameToken.Username" value: @"UPS_USERNAME"]; [json UpdateString: @"UPSSecurity.UsernameToken.Password" value: @"UPS_PASSWORD"]; [json UpdateString: @"UPSSecurity.ServiceAccessToken.AccessLicenseNumber" value: @"UPS_ACCESS_KEY"]; // Request all activity... [json UpdateString: @"TrackRequest.Request.RequestOption" value: @"activity"]; [json UpdateString: @"TrackRequest.Request.TransactionReference.CustomerContext" value: @"Your Test Case Summary Description"]; [json UpdateString: @"TrackRequest.InquiryNumber" value: @"1Z12345E0205271688"]; CkoStringBuilder *sb = [[CkoStringBuilder alloc] init]; CkoHttpResponse *resp = [http PostJson3: url contentType: @"application/json" json: json]; if (http.LastMethodSuccess != YES) { NSLog(@"%@",http.LastErrorText); return; } NSLog(@"%@%d",@"status = ",[resp.StatusCode intValue]); // A 200 response status indicate success. if ([resp.StatusCode intValue] != 200) { NSLog(@"%@",resp.BodyStr); NSLog(@"%@",@"Failed."); return; } [json Load: resp.BodyStr]; json.EmitCompact = NO; NSLog(@"%@",[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. NSString *statusCode = [json StringOf: @"TrackResponse.Response.ResponseStatus.Code"]; NSString *statusDescription = [json StringOf: @"TrackResponse.Response.ResponseStatus.Description"]; NSLog(@"%@%@",@"statusCode: ",statusCode); NSLog(@"%@%@",@"statusDescription",statusDescription); NSString *customerContext = [json StringOf: @"TrackResponse.Response.TransactionReference.CustomerContext"]; NSString *inquiryNumberCode = [json StringOf: @"TrackResponse.Shipment.InquiryNumber.Code"]; NSString *inquiryNumberDescription = [json StringOf: @"TrackResponse.Shipment.InquiryNumber.Description"]; NSString *inquiryNumberValue = [json StringOf: @"TrackResponse.Shipment.InquiryNumber.Value"]; NSString *shipmentTypeCode = [json StringOf: @"TrackResponse.Shipment.ShipmentType.Code"]; NSString *shipmentTypeDescription = [json StringOf: @"TrackResponse.Shipment.ShipmentType.Description"]; NSString *shipperNumber = [json StringOf: @"TrackResponse.Shipment.ShipperNumber"]; NSString *serviceCode = [json StringOf: @"TrackResponse.Shipment.Service.Code"]; NSString *serviceDescription = [json StringOf: @"TrackResponse.Shipment.Service.Description"]; NSString *referenceNumberCode = [json StringOf: @"TrackResponse.Shipment.ReferenceNumber.Code"]; NSString *referenceNumberValue = [json StringOf: @"TrackResponse.Shipment.ReferenceNumber.Value"]; NSString *pickupDate = [json StringOf: @"TrackResponse.Shipment.PickupDate"]; NSString *trackingNumber = [json StringOf: @"TrackResponse.Shipment.Package.TrackingNumber"]; NSString *unitOfMeasurementCode = [json StringOf: @"TrackResponse.Shipment.Package.PackageWeight.UnitOfMeasurement.Code"]; NSString *weight = [json StringOf: @"TrackResponse.Shipment.Package.PackageWeight.Weight"]; NSString *disclaimer = [json StringOf: @"TrackResponse.Disclaimer"]; int i = 0; int activityCount = [[json SizeOfArray: @"TrackResponse.Shipment.Package.Activity"] intValue]; NSLog(@"%@%d",@"activityCount: ",activityCount); while (i < activityCount) { NSLog(@"%@%d",@"-- activity ",i); json.I = [NSNumber numberWithInt: i]; if ([json HasMember: @"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City"] == YES) { NSString *city = [json StringOf: @"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City"]; NSLog(@"%@%@",@"city: ",city); NSString *provinceCode = [json StringOf: @"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.StateProvinceCode"]; NSString *postalCode = [json StringOf: @"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.PostalCode"]; NSString *countryCode = [json StringOf: @"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.CountryCode"]; } NSString *locationCode = [json StringOf: @"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Code"]; NSString *locationDescription = [json StringOf: @"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Description"]; NSString *locationSignedForByName = [json StringOf: @"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.SignedForByName"]; NSString *activityStatusType = [json StringOf: @"TrackResponse.Shipment.Package.Activity[i].Status.Type"]; NSLog(@"%@%@",@"activityStatusType: ",activityStatusType); NSString *activityStatusDescription = [json StringOf: @"TrackResponse.Shipment.Package.Activity[i].Status.Description"]; NSLog(@"%@%@",@"activityStatusDescription: ",activityStatusDescription); NSString *activityStatusCode = [json StringOf: @"TrackResponse.Shipment.Package.Activity[i].Status.Code"]; NSLog(@"%@%@",@"activityStatusCode: ",activityStatusCode); NSString *activityDate = [json StringOf: @"TrackResponse.Shipment.Package.Activity[i].Date"]; NSString *activityTime = [json StringOf: @"TrackResponse.Shipment.Package.Activity[i].Time"]; if ([json HasMember: @"TrackResponse.Shipment.Package.Activity[i].Document"] == YES) { int typeCode = [[json IntOf: @"TrackResponse.Shipment.Package.Activity[i].Document.Type.Code"] intValue]; NSString *typeDescription = [json StringOf: @"TrackResponse.Shipment.Package.Activity[i].Document.Type.Description"]; NSString *documentContent = [json StringOf: @"TrackResponse.Shipment.Package.Activity[i].Document.Content"]; NSString *documentFormatCode = [json StringOf: @"TrackResponse.Shipment.Package.Activity[i].Document.Format.Code"]; // Format description would be something like "GIF" for a signature image. NSString *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. CkoStringBuilder *sbImagePath = [[CkoStringBuilder alloc] init]; [sbImagePath Append: @"qa_output/sig_"]; [sbImagePath Append: trackingNumber]; [sbImagePath Append: @"."]; [sbImagePath Append: documentFormatDescription]; CkoBinData *imageData = [[CkoBinData alloc] init]; success = [imageData AppendEncoded: documentContent encoding: @"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"] intValue]; while (i < refnumCount) { json.I = [NSNumber numberWithInt: i]; NSString *refnumCode = [json StringOf: @"TrackResponse.Shipment.Package.ReferenceNumber[i].Code"]; NSString *refnumValue = [json StringOf: @"TrackResponse.Shipment.Package.ReferenceNumber[i].Value"]; i = i + 1; } NSLog(@"%@",@"Success."); |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.