Simple Adobe Flex Application

This simple Adobe Flex application will fetch the city and state information from a Yahoo HTTP Service when a valid zip code is provided. The source code below shows how easy it is to develop rich internet application using Adobe Flex. And with the upcoming release of Adobe AIR, which will bring internet applications to the desktop, there is no better time to learn Flex.

Sample response from Yahoo Http Service:

<ResultSet>
   <Result precision="zip">
      <Latitude>30.210741</Latitude>
      <Longitude>-81.542832</Longitude>
      <Address/>
      <City>Jacksonville</City>
      <State>FL</State>
      <Zip>32256</Zip>
      <Country>US</Country>
   </Result>
</ResultSet>

Source Code:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="300" height="200">
        <mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="300" height="200"
                          title="City and State Locator" horizontalAlign="center">
                <mx:Form width="100%" height="100%" horizontalCenter="0" verticalCenter="0">
                        <mx:FormItem label="Zipcode">
                                <mx:TextInput id="zipcode" change="getLocation()"/>
                        </mx:FormItem>
                        <mx:FormItem label="City">
                                <mx:TextInput id="city"/>
                        </mx:FormItem>
                        <mx:FormItem label="State">
                                <mx:TextInput id="state"/>
                        </mx:FormItem>
                </mx:Form>
        </mx:Panel>
        <mx:HTTPService
                id="getLocationService"
                url="http://local.yahooapis.com/MapsService/V1/geocode"
                useProxy="false"
                method="POST"
                result="event.token.resultHandler( event );"
                fault="event.token.faultHandler( event );" >
        </mx:HTTPService>
        <mx:Script>
                <![CDATA[
                        import mx.rpc.AsyncToken;
                        public function getLocation(): void
                        {
                                if(zipcode.length != 5)
                                {
                                        city.text = "";
                                        state.text = "";
                                        return;
                                }
                               
                                var token : AsyncToken = getLocationService.send({appid:"YahooDemo", zip:zipcode.text});
                                token.resultHandler = onResult;
                        }
                       
                        public function onResult( event : * = null ) : void
                        {
                                var c:String = event.result.ResultSet.Result.City;
                                var s:String = event.result.ResultSet.Result.State;
                                var z:String = event.result.ResultSet.Result.Zip;
                               
                                city.text = c;
                                state.text = s;
                                zipcode.text = z;
                        }
                ]]>
        </mx:Script>
</mx:Application>