| Jinlong 的个人资料Derek's Home照片日志列表 | 帮助 |
|
Derek's Home7月30日 Accessing A Web Service by Flex BuilderFlex 101: Accessing A Web Service For this post, I decided to change things up a bit. Rather than go explore complex application patterns or data visualization, we're going back to basics. We will be covering how to make a basic application that makes a call to a web service and retrieves data. In this example, I am using a the "Phone Number Lite" free web service from StrikeIron, which will return the city, state, county, and county for any area code + first 3 digits of a US telephone number. You can use this to try and figure out those pesky number that keep calling you (or, just get caller ID). Before digging into the code and seeing how everything works together, let's take a look at the final product. Enter an area code, and the three-digit phone number prefix, and click on the "Find" button to fetch results. For example, a phone number in the format 301-929-XXXX would be populated as shown below. Note: These services are limited to 100 requests per day, per IP address. Now, let's take a look at the application. The following code segment shows the visual components of the application as declared in MXML. There is an ApplicationControlBar, which contains two labels, two text input boxes, and a button. There is also a progress bar, and a text area that will be used to display the result to the user. Notice that the progress bar's visibility and layout inclusion are bound to the searching Boolean value. This indicates that the the progress bar will not be visible when the application is not searching. Responder ans AsynchToken classes, but we'll keep it simple for now. search() function is invoked when the "Find" button is clicked. This sets the searching Boolean value to true, so that the progress bar is displayed. It then invokes the GetLocationInfoForPhoneNumbers operation on the web service, passing in the parameter values from the area code and digits text boxes. onResult function gets invoked when the web service result returns, and the onFault function would get invoked if there was an error when invoking the web service. In either case, the searching Boolean gets set to false, hiding the progress bar, and then the appropriate action is taken. If there was a fault, then the fault message gets written to the output text area. If there was a valid result, then the result is parsed and written into the output text area. In the onResult function, you may notice that it is checking for a event.result.ServiceResult.Count object. This is specific to the service result in this example; it is not like this for every web service. In this case, it is used to determine if information could be found for the specified area code and prefix. If none was specified (count == 0), then the status message is displayed. If there is a result returned, the value is parsed into a string, and shown in the output text area. http://www.tricedesigns.com/portfolio/phoneNumber/srcview/Phone Number Lookup.zip |
|
|||
|
|