RSS订阅优然探索
你的位置:首页 » Flex » 正文

使用 HTTPService 加载外部数据

选择字号: 超大 标准 发布时间:2010-2-19 10:24:42 | 作者:admin | 0个评论 | 人浏览

<?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>

 

标签:

猜你喜欢

发表评论

必填

选填

选填

必填,不填不让过哦,嘻嘻。

记住我,下次回复时不用重新输入个人信息

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。