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.

 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

0 Responses to Loading name/value pairs using the mx:HTTPService tag

  1. Damon says:

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

  2. peterd says:

    Damon,

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

    Peter

  3. Joy says:

    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

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree