
112 Retrieve and Display Data
Insert a HTTPService component
For the blog reader in this lesson, you retrieve posts from Matt Chotin’s blog at http://
www.adobe.com/go/mchotinblog on a news aggregator website. Matt is a product manager
on the Flex team and writes about Flex in his blog.
You can use the HTTPService component to access the blog’s XML feed and retrieve
information about recent posts. The component lets you send an HTTP GET or POST
request, and then retrieve the data returned in the response.
1. In Source mode, enter the following <mx:HTTPService> tag immediately after the opening
<mx:Application> tag:
<mx:HTTPService
id="feedRequest"
url="http://weblogs.macromedia.com/mchotin/index.xml"
useProxy="false"/>
The url property specifies the location of the requested file, in this case the RSS 2.0 feed
of Matt Chotin’s blog. As of this writing, the URL was still valid, but you should check to
make sure it hasn’t changed. You should use the latest RSS 2.0 URL listed on the right side
of the blog at http://www.adobe.com/go/mchotinblog.
The
useProxy property specifies that you don’t want to use a proxy on a server. The
domain where Matt’s blog is located has a crossdomain.xml setup, so Flash Player can
access the remote data sources on this server, including RSS feeds. For more information,
see “Review your access to remote data sources” on page 108.
The next step is to prompt the application to send a request to the specified URL. You
decide to send the request automatically whenever the application starts, as follows.
2. In the <mx:Application> tag, add the following creationComplete property (in bold):
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" creationComplete="feedRequest.send()">
When your application is finished starting up, the HTTPService component’s send()
method is called. The method makes an HTTP GET or POST request to the specified
URL, and an HTTP response is returned. In this case, the RSS feed returns XML data.
Next, you want to check if the application is retrieving the RSS feed successfully. You can
do this by binding data to the Label control, as follows.
NOTE
If you want, you can also use the following alias in the component: http://
www.adobe.com/go/flex_blogfeed.
Commentaires sur ces manuels