Alpha DevCon 2018
Results 1 to 12 of 12

Thread: Zip Code Lookup in Phone Gap

  1. #1
    Member
    Real Name
    Darol Harrison
    Join Date
    Aug 2011
    Location
    Arkansas
    Posts
    45

    Default Zip Code Lookup in Phone Gap

    I was trying different things with Phone Gap and created a simple UX control with the Zip Code Lookup Action JavaScript. It works great on my computer, but when I put it on my phone using Phone Gap it does not work. It is apparently not connecting to get the information. I am on wifi and have no other problems with my phone. Any ideas?

  2. #2
    Member jgrannis's Avatar
    Real Name
    Jeff Grannis
    Join Date
    Apr 2016
    Location
    New Iberia, LA
    Posts
    238

    Default Re: Zip Code Lookup in Phone Gap

    Put the URL for All Ajax Callbacks that you configured in your PhoneGap settings directly into a web browser on the phone. Is the web browser finding the address (ie. hitting the web server/app server)?
    Do you see the default page for that URL? Or maybe some other http status message?

  3. #3
    Member
    Real Name
    Darol Harrison
    Join Date
    Aug 2011
    Location
    Arkansas
    Posts
    45

    Default Re: Zip Code Lookup in Phone Gap

    I can get to the website (USPS Tools Zip Code Lookup) from my phone's browser. Since the app works on my computer I don't see why the phone wouldn't do the same. I appreciate the questions and will dig deeper into the phone gap settings, which is where I am assuming the issue lies.

  4. #4
    "Certified" Alphaholic
    Real Name
    David Kates
    Join Date
    Apr 2008
    Location
    Unionville, ON
    Posts
    6,798

    Default Re: Zip Code Lookup in Phone Gap

    I wouldn't bother with that action for a PhoneGap Build app as it makes a callback to the Alpha server. I'd prefer to go directly to USPS and use their API.

    Go here... https://www.usps.com/business/web-to...is/welcome.htm and Register for a Username.

    Then you can make a call directly to USPS and get back the data you want.

    Supplying Address, City, State... you should get back xml like this... which includes Zip5 and Zip4.

    Code:
    "<?xml version="1.0" encoding="UTF-8"?>
    <AddressValidateResponse><Address ID="1"><Address2>8 WILDWOOD DR</Address2><City>OLD LYME</City><State>CT</State><Zip5>06371</Zip5><Zip4>1844</Zip4></Address></AddressValidateResponse>"
    Code:
    var address = {dialog.Object}.getValue('Address');
    var city = {dialog.Object}.getValue('City');
    var state = {dialog.Object}.getValue('State');
    
    var zipURL = 'http://production.shippingapis.com/ShippingAPI.dll?API=Verify&XML=<AddressValidateRequest USERID="YOUR_USERNAME_GOES_HERE"><Address ID="1"><Address1></Address1><Address2>' + address + '</Address2><City>' + city + '</City><State>' + state + '</State><Zip5></Zip5><Zip4></Zip4></Address></AddressValidateRequest>';
    
    fetch(zipURL).then(function(response) {
    	if (response.status !== 200) {
    		console.log('Looks like there was a problem. Status Code: ' +
    		response.status);
    		return;
    	}
    	return response.text();
    }).then(function(text) {
    		var zip = jQuery(text).find("Zip5").text();
    		{dialog.Object}.setValue('Zipcode',zip,false);
    
    }).catch(function(err) {
    	console.log('Fetch Error :-S', err);
    });
    This works under PhoneGap Build in iOS and Android. You'd need to add a bit more error checking in case no Zip comes back.

  5. #5
    Member
    Real Name
    Darol Harrison
    Join Date
    Aug 2011
    Location
    Arkansas
    Posts
    45

    Default Re: Zip Code Lookup in Phone Gap

    Thanks David. I will try what you have provided. My main interest was trying to understand if there was something I was doing wrong on the phone gap build for when I put the real app out there. The zip code one was my test case. It is my understanding the action javascript goes to the USPS web site direct so I expected it to work on the phone app. Appreciate the effort and what you have provided will be a good training effort for me.

  6. #6
    "Certified" Alphaholic
    Real Name
    David Kates
    Join Date
    Apr 2008
    Location
    Unionville, ON
    Posts
    6,798

    Default Re: Zip Code Lookup in Phone Gap

    I don't think the Action Javascript makes a direct call to USPS. Open the dialog where that Action Javascript is defined... not the action itself where you define it's parameters, but just where the call is. You have a Radio Button at the top labelled Action Javascript... which is selected. Now... select the other Radio Button choice... Text mode. This turns the Action into code. You'll see that an ajax callback is being made to an XBasic function named "ZipCodeLookup". I don't know where that is... or what it does... it's not documented... but it is an XBasic function of a callback.

    So... you're going from Client - to Server - to USPS - to Sever - to Client.

    This Action does work for me... so your problem could be...

    You have not published your UX to your Alpha Server.

    You have security turned on for your server... but you're not logging in... or your UX security is set to always denied. Most of the time, security for a PhoneGap Build app, should be set to Always Allowed for the UX.

    Your Alpha PhoneGap Build builder property "URL For All Ajax Callbacks" is not set... or not set properly.

  7. #7
    Member compuaid's Avatar
    Real Name
    Brad Weaver
    Join Date
    Aug 2007
    Location
    Ottawa, ON
    Posts
    365

    Default Re: Zip Code Lookup in Phone Gap

    David,

    I'm trying to implement your code in a standard UX component for a browser based component (not Phonegap or mobile) and I when I debug the JavaScript I get an error that fetch is not defined. Is this a PhoneGap framework method and if so is there an equivalent way to accomplish this call for a regulare web-based client?
    Brad Weaver, President
    ComputerAid International
    Ottawa ON Canada
    Versailles KY USA
    www.compuaid.com

  8. #8
    "Certified" Alphaholic
    Real Name
    David Kates
    Join Date
    Apr 2008
    Location
    Unionville, ON
    Posts
    6,798

    Default Re: Zip Code Lookup in Phone Gap

    Nope... the fetch API is built into the browser... do a search... you'll find some interesting info about it. You've not detailed what version/build of Alpha your using or the browser version/build... fetch hasn't always been around. You could use XMLHttpRequest instead. I just tried that previously posted code and it runs without error under Chrome and Edge.

  9. #9
    "Certified" Alphaholic CharlesParker's Avatar
    Real Name
    Charles Parker
    Join Date
    Dec 2012
    Location
    New Orleans, LA
    Posts
    1,871

    Default Re: Zip Code Lookup in Phone Gap

    As an alternative, why not use the Google auto-suggest field type which returns loads of info including the zip code?
    NWCOPRO Nuisance Wildlife Control Software-My Application: http://www.nwcopro.com "I am not discouraged, because every wrong attempt discarded is another step forward."

  10. #10
    "Certified" Alphaholic
    Real Name
    David Kates
    Join Date
    Apr 2008
    Location
    Unionville, ON
    Posts
    6,798

    Default Re: Zip Code Lookup in Phone Gap

    Quote Originally Posted by CharlesParker View Post
    As an alternative, why not use the Google auto-suggest field type which returns loads of info including the zip code?
    That sounds interesting... could you provide a bit of detail on how/where to use it?

  11. #11
    "Certified" Alphaholic CharlesParker's Avatar
    Real Name
    Charles Parker
    Join Date
    Dec 2012
    Location
    New Orleans, LA
    Posts
    1,871

    Default Re: Zip Code Lookup in Phone Gap

    Sure, I first went into the actual builder for it and realized I needed some additional fields in the database because truly it returns ALOT of information and since it is based on the users location (or can be) as the user types in teh address and clicks it ALL of the returned info goes into the other fields, including geocodes!
    So in my use case, a user gets a phone call and wants to enter in the address - this makes it VERY easy. I can then use the geocodes for mapping purposes.
    I don’t know if that answers your question, but that is how I use it. It’s just a text field. Since I now use that I no longer have to make the extra call for the zip code it’s already there.
    NWCOPRO Nuisance Wildlife Control Software-My Application: http://www.nwcopro.com "I am not discouraged, because every wrong attempt discarded is another step forward."

  12. #12
    Member compuaid's Avatar
    Real Name
    Brad Weaver
    Join Date
    Aug 2007
    Location
    Ottawa, ON
    Posts
    365

    Default Re: Zip Code Lookup in Phone Gap

    Thanks, guys. Building on David's JavaScript code, I created an Xbasic callback function that's working well now with the new API.

    Here's sample code (assuming you have fields named Mailing_Address, City, State, Postal_Code in your UX.

    Code:
    function PostOffice as c (e as p)
    	
    dim Response as c = ""
    dim ResponseTimeout as n = 30000 '30 seconds
    dim WebURI as c
    dim content as p
    dim vczip as c = ""
    dim vplus4 as c = ""
    dim USPS_Zipcode_Lookup2 as c
    
    WebURI= "http://production.shippingapis.com/ShippingAPI.dll?API=Verify&XML=<AddressValidateRequest USERID=%22YourUSPSUserID%22><Address ID=%221%22><Address1></Address1><Address2>%20"
    WebURI = WebURI+alltrim(e.dataSubmitted.mailing_address)+"</Address2><City>"+alltrim(e.dataSubmitted.city)+"</City><State>"+alltrim(e.dataSubmitted.state)+"</State><Zip5></Zip5><Zip4></Zip4></Address></AddressValidateRequest>"
    
    Response = http_post_page2(WebURI,"",.f.,ResponseTimeout)
    *property_from_xml(response,content,.t.)
    
    IF eval_valid("content.AddressValidateResponse.Address[1].Zip5[1].__A5_elementContent")
    	vczip = content.AddressValidateResponse.Address[1].Zip5[1].__A5_elementContent
    END IF
    	
    IF eval_valid("content.AddressValidateResponse.Address[1].Zip4[1].__A5_elementContent")
    	vplus4 = content.AddressValidateResponse.Address[1].Zip4[1].__A5_elementContent
    END IF
    
    USPS_Zipcode_Lookup2 = ""
    
    if vczip = ""
    	resultmsg = "Could not locate address.  No change made"
    else
    	if vplus4 = ""
    		USPS_Zipcode_Lookup2= vczip
    	else
    		USPS_Zipcode_Lookup2= vczip+"-"+vplus4
    	end if
    	resultmsg = "Address found.  Postal code has been verified."
            e._set.postal_code.value = USPS_Zipcode_Lookup2
    end if
    PostOffice = "alert('"+resultmsg+"');"
    
    END FUNCTION
    Brad Weaver, President
    ComputerAid International
    Ottawa ON Canada
    Versailles KY USA
    www.compuaid.com

Similar Threads

  1. Active Phone number on smart phone?
    By kingcarol in forum Mobile & Browser Applications
    Replies: 30
    Last Post: 06-21-2013, 03:02 PM
  2. Zip Code Lookup
    By Dennis22 in forum Alpha Five Version 8
    Replies: 12
    Last Post: 08-18-2008, 06:37 AM
  3. Replies: 10
    Last Post: 08-01-2007, 09:33 PM
  4. Zip Code Lookup Help
    By Jarrett Nahon in forum Alpha Five Version 5
    Replies: 5
    Last Post: 08-28-2003, 06:46 PM
  5. lookup zip code
    By Harold Jacubowitz in forum Alpha Five Version 4
    Replies: 4
    Last Post: 03-06-2001, 11:07 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •