<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:aral="*"
creationComplete="photoService.send();"
viewSourceURL="src/HTTPServiceConsumingFlickrRSSFeed/index.html">
<!-- Model -->
<!-- Define the HTTP service -->
<mx:HTTPService
id="photoService"
url="http://api.flickr.com/services/feeds/photos_public.gne"
resultFormat="e4x"
result="photoResultHandler(event);"
fault="photoFaultHandler(event);"
/>
<!-- Controller -->
<mx:Script>
<![CDATA[
import flash.net.navigateToURL;
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
[Bindable]
private var photoFeed:XML;
// Define and use atom namespace.
private namespace atom = "http://www.w3.org/2005/Atom";
use namespace atom;
// The image URL is in the HTML content
// so we have to parse it out.
private function parseImageUrl(fromHtml:XMLList):String
{
var pattern:RegExp = /img src="(.+?)" /;
var results:Array = pattern.exec(fromHtml);
var imageURL:String = results[1]; // backreference 1 from pattern
return imageURL;
}
// Result handler - gets called after RSS is loaded.
private function photoResultHandler(event:ResultEvent):void
{
photoFeed = event.result as XML;
}
// Fault handler - displays the error.
private function photoFaultHandler(event:FaultEvent):void
{
Alert.show(event.fault.message, "Could not load photo feed");
}
// Opens author's HTML page in external window.
private function openAuthorPage(event:MouseEvent):void
{
var dataIndex:uint = Number(event.target.instanceIndices.toString());
var urlRequest:URLRequest = new URLRequest(photoFeed.entry[dataIndex].author.uri)
navigateToURL(urlRequest, "_blank");
}
]]>
</mx:Script>
<!-- View -->
<!-- Effects -->
<mx:Fade id="fadeIn" duration="3000" alphaFrom="0" alphaTo="1"/>
<mx:Fade id="fadeOut" duration="3000" alphaFrom="1" alphaTo="0"/>
<!-- User Interface -->
<mx:Panel title="Public photos" width="100%" height="100%">
<mx:Tile width="100%" height="100%">
<mx:Repeater id="photos" dataProvider="{photoFeed.entry}">
<mx:VBox
xmlns:mx="http://www.adobe.com/2006/mxml"
horizontalAlign="center"
>
<mx:Image
id="myImage"
source="{parseImageUrl(photos.currentItem.content)}"
completeEffect="{fadeIn}"
/>
<mx:Text text="{photos.currentItem.title}"/>
<mx:LinkButton
label="{photos.currentItem.author.name}"
click="openAuthorPage(event);"
/>
</mx:VBox>
</mx:Repeater>
</mx:Tile>
<mx:ControlBar horizontalAlign="center">
<mx:Button label="Update" click="photoService.send();"/>
</mx:ControlBar>
</mx:Panel>
</mx:Application>