28
Jul
07

Loading name/value pairs using the mx:HTTPService tag

Semi-related to my previous post, I was playing around and figured out a way (probably not the best method) for loading a page of remote name/value pairs and putting them in a DataGrid using the HTTPService tag.

Full code after the jump

 

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal" verticalAlign="middle" backgroundColor="white" creationComplete="httpParams.send()">  

    <mx:HTTPService resultFormat="flashvars" url="{VARIABLES_URL}" id="httpParams" result="onResult(event)" />  

    <mx:Script>
        <![CDATA[
            import mx.rpc.events.ResultEvent;
            import mx.collections.ArrayCollection;  

            [Bindable]
            private var VARIABLES_URL:String = "http://www.flash-mx.com/mm/params.txt";  

            [Bindable]
            private var paramColl:ArrayCollection = new ArrayCollection();  

            private function onResult(evt:ResultEvent):void {
                var vars:Object = evt.result;
                var key:String;  

                for (key in vars) {
                    paramColl.addItem({key:key, value:vars[key]});
                }
                params.visible = true;
            }
        ]]>
    </mx:Script>  

    <mx:VBox>
        <mx:Label text="Parameters:" />
        <mx:DataGrid id="params" dataProvider="{paramColl}" rowCount="5" visible="false">
            <mx:columns>
                <mx:DataGridColumn dataField="key" headerText="Key" />
                <mx:DataGridColumn dataField="value" headerText="Value" />
            </mx:columns>
        </mx:DataGrid>
    </mx:VBox>  

</mx:Application>

Again, I’m sure there is a nicer way of handling the ResultEvent and converting to a data provider, so you may want to play around with it a bit.

Of course, if you know the names of the parameters in the file that you’re loading, you could also just do something like this:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white" creationComplete="httpService.send()"> 

    <mx:HTTPService id="httpService" resultFormat="flashvars" url="params.txt" /> 

    <mx:VBox>
        <mx:Label text="name: {httpService.lastResult.name}" />
        <mx:Label text="product: {httpService.lastResult.product}" />
        <mx:Label text="powermove: {httpService.lastResult.powermove}" />
        <mx:Label text="skill: {httpService.lastResult.skill}" />
    </mx:VBox> 

</mx:Application>

Oh, and remember, since the params.txt file is being loaded at run-time and embedded during compile-time, the params.txt file needs to be relative to the SWF file and not the MXML file.


3 Responses to “Loading name/value pairs using the mx:HTTPService tag”


  1. 1 Damon Mar 28th, 2008 at 1:52 pm

    I’d love to see what your “params.txt” file looks like in this example?
    Thanks for your awesome site! Very helpful.

  2. 2 peterd Mar 28th, 2008 at 2:05 pm

    Damon,

    I’ll try and update the example later, but for now check out http://www.flash-mx.com/mm/params.txt

    Peter

  3. 3 Joy Apr 21st, 2008 at 7:14 pm

    Is there a example of the net, where you can read the response from a Java Servlet? I have a requirement where i have to write text in XML format on ServletOutputStream and read it in Flex.

    Joy

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".




July 2007
M T W T F S S
    Aug »
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Badge Farm

  • Firefox 2
  • Powered by Redoable 1.2
  • Feeds burnt by Feedburner
  • Feed