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™) UPS Address Validation (City, State, Zip)Demonstrates making a call to the UPS address validation REST API.
// 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 requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. boolean success; CkHttp http = new CkHttp(); String 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. CkJsonObject json = new CkJsonObject(); json.UpdateString("AccessRequest.AccessLicenseNumber","UPS_ACCESS_KEY"); json.UpdateString("AccessRequest.UserId","UPS_USERNAME"); json.UpdateString("AccessRequest.Password","UPS_PASSWORD"); json.UpdateString("AddressValidationRequest.Request.TransactionReference.CustomerContext","Your Customer Context"); json.UpdateString("AddressValidationRequest.Request.RequestAction","AV"); json.UpdateString("AddressValidationRequest.Address.City","ALPHARETTA"); // We're making an intentional mistake here by passing CA instead of GA. json.UpdateString("AddressValidationRequest.Address.StateProvinceCode","CA"); json.UpdateString("AddressValidationRequest.Address.PostalCode","30005"); CkStringBuilder sb = new CkStringBuilder(); CkHttpResponse resp = http.PostJson3(url,"application/json",json); if (http.get_LastMethodSuccess() != true) { Log.i(TAG, http.lastErrorText()); return; } Log.i(TAG, "status = " + String.valueOf(resp.get_StatusCode())); // A 200 response status indicate success. if (resp.get_StatusCode() != 200) { Log.i(TAG, resp.bodyStr()); Log.i(TAG, "Failed."); return; } json.Load(resp.bodyStr()); json.put_EmitCompact(false); Log.i(TAG, 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. String customerContext; String statusCode; String statusDescription; String resultRank; String resultQuality; String city; String provinceCode; String postalCodeLowEnd; String postalCodeHighEnd; String rank; String quality; String addressCity; String addressStateProvinceCode; int numResults = json.SizeOfArray("AddressValidationResponse.AddressValidationResult"); 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"); Log.i(TAG, "Exact match!"); Log.i(TAG, "postal code: " + postalCodeLowEnd); } else { Log.i(TAG, "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.put_I(i); rank = json.stringOf("AddressValidationResponse.AddressValidationResult[i].Rank"); Log.i(TAG, "rank: " + rank); quality = json.stringOf("AddressValidationResponse.AddressValidationResult[i].Quality"); addressCity = json.stringOf("AddressValidationResponse.AddressValidationResult[i].Address.City"); Log.i(TAG, "addressCity: " + addressCity); addressStateProvinceCode = json.stringOf("AddressValidationResponse.AddressValidationResult[i].Address.StateProvinceCode"); postalCodeLowEnd = json.stringOf("AddressValidationResponse.AddressValidationResult[i].PostalCodeLowEnd"); Log.i(TAG, "postal code: " + postalCodeLowEnd); postalCodeHighEnd = json.stringOf("AddressValidationResponse.AddressValidationResult[i].PostalCodeHighEnd"); 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.