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 Address Validation (City, State, Zip)Demonstrates making a call to the UPS address validation REST API.
#import <CkoHttp.h> #import <NSString.h> #import <CkoJsonObject.h> #import <CkoStringBuilder.h> #import <CkoHttpResponse.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 address validation: NSString *url = @"https://wwwcie.ups.com/rest/AV"; // Send an HTTP request with the following JSON body: // { // "AccessRequest": { // "AccessLicenseNumber": "Your Access License Number", // "UserId": "Your Username", // "Password": "Your Password" // }, // "AddressValidationRequest": { // "Request": { // "TransactionReference": { // "CustomerContext": "Your Customer Context" // }, // "RequestAction": "AV" // }, // "Address": { // "City": "ALPHARETTA", // "StateProvinceCode": "GA", // "PostalCode": "30005" // } // } // } // Build the above JSON. CkoJsonObject *json = [[CkoJsonObject alloc] init]; [json UpdateString: @"AccessRequest.AccessLicenseNumber" value: @"UPS_ACCESS_KEY"]; [json UpdateString: @"AccessRequest.UserId" value: @"UPS_USERNAME"]; [json UpdateString: @"AccessRequest.Password" value: @"UPS_PASSWORD"]; [json UpdateString: @"AddressValidationRequest.Request.TransactionReference.CustomerContext" value: @"Your Customer Context"]; [json UpdateString: @"AddressValidationRequest.Request.RequestAction" value: @"AV"]; [json UpdateString: @"AddressValidationRequest.Address.City" value: @"ALPHARETTA"]; // We're making an intentional mistake here by passing CA instead of GA. [json UpdateString: @"AddressValidationRequest.Address.StateProvinceCode" value: @"CA"]; [json UpdateString: @"AddressValidationRequest.Address.PostalCode" value: @"30005"]; 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]); // A successful exact response looks like this: // { // "AddressValidationResponse": { // "Response": { // "TransactionReference": { // "CustomerContext": "Your Customer Context" // }, // "ResponseStatusCode": "1", // "ResponseStatusDescription": "Success" // }, // "AddressValidationResult": { // "Rank": "1", // "Quality": "1.0", // "Address": { // "City": "ALPHARETTA", // "StateProvinceCode": "GA" // }, // "PostalCodeLowEnd": "30005", // "PostalCodeHighEnd": "30005" // } // } // } // // A successful response that was not an exact match provides an array of closest matches, like this: // { // "AddressValidationResponse": { // "Response": { // "TransactionReference": { // "CustomerContext": "Your Customer Context" // "Quality": "0.9875", // "Address": { // }, // "ResponseStatusCode": "1", // "ResponseStatusDescription": "Success" // }, // "AddressValidationResult": [ // { // "Rank": "1", // "City": "ALPHARETTA", // "StateProvinceCode": "GA" // }, // "PostalCodeLowEnd": "30005", // "PostalCodeHighEnd": "30005" // }, // { // "Rank": "2", // "Quality": "0.9750", // "Address": { // "City": "ALPHARETTA", // "StateProvinceCode": "GA" // }, // "PostalCodeLowEnd": "30004", // "PostalCodeHighEnd": "30004" // }, // { // "Rank": "3", // "Quality": "0.9750", // "Address": { // "City": "ALPHARETTA", // "StateProvinceCode": "GA" // }, // "PostalCodeLowEnd": "30009", // "PostalCodeHighEnd": "30009" // } // ] // } // } // Use the online tool at Generate JSON Parsing Code // to generate JSON parsing code. NSString *customerContext = 0; NSString *statusCode = 0; NSString *statusDescription = 0; NSString *resultRank = 0; NSString *resultQuality = 0; NSString *city = 0; NSString *provinceCode = 0; NSString *postalCodeLowEnd = 0; NSString *postalCodeHighEnd = 0; NSString *rank = 0; NSString *quality = 0; NSString *addressCity = 0; NSString *addressStateProvinceCode = 0; int numResults = [[json SizeOfArray: @"AddressValidationResponse.AddressValidationResult"] intValue]; if (numResults < 0) { // Here's parse code for the above JSON exact response: customerContext = [json StringOf: @"AddressValidationResponse.Response.TransactionReference.CustomerContext"]; statusCode = [json StringOf: @"AddressValidationResponse.Response.ResponseStatusCode"]; statusDescription = [json StringOf: @"AddressValidationResponse.Response.ResponseStatusDescription"]; resultRank = [json StringOf: @"AddressValidationResponse.AddressValidationResult.Rank"]; resultQuality = [json StringOf: @"AddressValidationResponse.AddressValidationResult.Quality"]; city = [json StringOf: @"AddressValidationResponse.AddressValidationResult.Address.City"]; provinceCode = [json StringOf: @"AddressValidationResponse.AddressValidationResult.Address.StateProvinceCode"]; postalCodeLowEnd = [json StringOf: @"AddressValidationResponse.AddressValidationResult.PostalCodeLowEnd"]; postalCodeHighEnd = [json StringOf: @"AddressValidationResponse.AddressValidationResult.PostalCodeHighEnd"]; NSLog(@"%@",@"Exact match!"); NSLog(@"%@%@",@"postal code: ",postalCodeLowEnd); } else { NSLog(@"%@",@"Non-Exact match."); customerContext = [json StringOf: @"AddressValidationResponse.Response.TransactionReference.CustomerContext"]; statusCode = [json StringOf: @"AddressValidationResponse.Response.ResponseStatusCode"]; statusDescription = [json StringOf: @"AddressValidationResponse.Response.ResponseStatusDescription"]; int i = 0; while (i < numResults) { json.I = [NSNumber numberWithInt: i]; rank = [json StringOf: @"AddressValidationResponse.AddressValidationResult[i].Rank"]; NSLog(@"%@%@",@"rank: ",rank); quality = [json StringOf: @"AddressValidationResponse.AddressValidationResult[i].Quality"]; addressCity = [json StringOf: @"AddressValidationResponse.AddressValidationResult[i].Address.City"]; NSLog(@"%@%@",@"addressCity: ",addressCity); addressStateProvinceCode = [json StringOf: @"AddressValidationResponse.AddressValidationResult[i].Address.StateProvinceCode"]; postalCodeLowEnd = [json StringOf: @"AddressValidationResponse.AddressValidationResult[i].PostalCodeLowEnd"]; NSLog(@"%@%@",@"postal code: ",postalCodeLowEnd); postalCodeHighEnd = [json StringOf: @"AddressValidationResponse.AddressValidationResult[i].PostalCodeHighEnd"]; i = i + 1; } } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.