6 Replies Latest reply on Nov 15, 2007 8:43 PM by ntsiii

    HTTPService question

      I am having problems getting the data I need out of a HTTPService request. I think the problem is to do with casting the result of the request into a type that the data provider can use. My code looks like this:

      [Bindable] public var goldList:XMLList;
      [Bindable] public var goldData:XML;

      private function initializeData():void
      goldData = goldRequest.lastResult.GoldPrice;
      goldList = goldRequest.lastResult.GoldPrice.Price;

      url=" http://dgcsc.org/goldprices.xml" />

      <mx:DataGrid x="20" y="80" id="goldGrid" width="400" dataProvider="{goldData}" >
      <mx:DataGridColumn headerText="Name" dataField="@currencyname" />
      <mx:DataGridColumn headerText="Price" dataField="Price" />

      If I change the dataProvider from dataProvider="{goldData}" to dataProvider="{goldRequest.lastResult.GoldPrice}" the Price information is displayed, but I need to do some formatting on the data before this in AS.

      The XML file returned from the goldRequest looks like this:

      <GoldPrice per="gram" date="2007-11-15 14:26:53">
      <Price currencyname="Argentine Peso" currencycode="ARS">79.5816</Price>
      <Price currencyname="Australian Dollar" currencycode="AUD">28.6555</Price>
      <Price currencyname="Brazilian Real" currencycode="BRL">44.3168</Price>

        • 1. Re: HTTPService question
          JKohn99 Level 1
          add a result handler to your httpService.

          Note that is this case I set the resultFormat to e4x so I can deal directly with the xml. resultFormat = object may not always work because it makes decisions as to how to structure the xml into as objects. Also e4x xml classes make parsing xml really easy.
          url=" http://lp008400:8080/FlexServletProject/servlet/FlexServlet"
          resultFormat="e4x" useProxy="false">


          then add the result handler:

          private function handleResult(event:ResultEvent):void
          var resultXml:XML = event.result as XML;
          ta.text = resultXml;
          var childXml:XMLList = resultXml.child("child");

          • 2. Re: HTTPService question
            Use the result event of HTTPService

            url=" http://dgcsc.org/goldprices.xml" />

            Then in the script you can define the method

            private function doSomething():void
            goldData = goldRequest.lastResult.GoldPrice;
            goldList = goldRequest.lastResult.GoldPrice.Price;
            • 3. Re: HTTPService question
              ntsiii Level 3
              Also, set resultFormat="e4x". "object" converts you xml into a nested, dynamic object structure. You have no control over this conversion, it is costly in performance, and the weakly typed objects are no advantage over using xml straight.

              If you want, loop through the xml and build stronly typed VOs.

              Below are some snippets.

              Sample code using HTTPService, e4x, handler function to populate a list item.
              Also shows usage of AsyncToken.

              The DataGrid tag:
              <mx:DataGrid id="dg" dataProvider="{_xlcMyListData}" .../>

              The HTTPService tag:
              <mx:HTTPService id="service" resultFormat="e4x" result="onResult(event)" fault="..../>

              Script block declaration:
              import mx.rpc.Events.ResultEvent;
              [Bindable]private var _xlcMyListData:XMLListCollection;

              Invoke send:
              var oRequest:Object = new Object();
              oRequest.Arg1 = "value1";
              var callToken:AsyncToken = service.send(oRequest);
              token.callId = "myQuery1";

              Result Handler function:
              private function onResult(oEvent:ResultEvent):void {
              var xmlResult:XML = XML(event.result); //converts result Object to XML. can also use "as" operator
              var xlMyListData:XMLList = xmlResult.myListData; //depends on xml format, is row data
              _xlcMyListData = new XMLListCollection(xlMyListData); //wrap the XMLList in a collection
              trace(_xlcMyListData.toXMLString()); //so you can see exactly how to specify dataField or build labelFunction
              var callToken:AsyncToken = oEvent.token;
              var sCallId = callToken.callId; //"myQuery1"
              switch(sCallId) { //Process the result conditionally
              case "myQuery1":
              doQuery2(); //do whatever. this example calls another data service query
              • 4. Re: HTTPService question
                edzillion Level 1
                thanks for the help. my next question will probably make it obvious that I'm new to this :)
                I used the event handler that JKOHN suggested:

                url=" http://dgcsc.org/goldprices.xml" />

                private function handleResult(event:ResultEvent):void
                goldData = event.result as XML;

                But my DataGrid:

                <mx:DataGrid x="10" y="50" id="goldPriceGrid" width="435"
                <mx:DataGridColumn headerText="Currency" dataField=""/>
                <mx:DataGridColumn headerText="Price" dataField="Price" width="100"/>

                displays the whole tag, rather than the data inside the price tag:

                <Price currencyname="Argentine Peso" currencycode="ARS">79.2705</Price>
                <Price currencyname="Australian Dollar" currencycode="AUD">28.645</Price>
                <Price currencyname="Brazilian Real" currencycode="BRL">44.3468</Price>
                <Price currencyname="Canadian Dollar" currencycode="CAD">24.9927</Price>
                <Price currencyname="Swiss Franc" currencycode="CHF">28.4799</Price>
                <Price currencyname="Yuan Renminbi" currencycode="CNY">188.2707</Price>
                <Price currencyname="Colombian Peso" currencycode="COP">55419.5439</Price>
                <Price currencyname="Euro" currencycode="EUR">17.3551</Price>
                <Price currencyname="Pound Sterling" currencycode="GBP">12.4149</Price>
                <Price currencyname="Hong Kong Dollar" currencycode="HKD">197.4677</Price>
                <Price currencyname="Indonesian Rupiah" currencycode="IDR">235078.7356</Price>
                <Price currencyname="Indian Rupee" currencycode="INR">995.1363</Price>
                <Price currencyname="Yen" currencycode="JPY">2802.7895</Price>
                <Price currencyname="Kuwaiti Dinar" currencycode="KWD">7.005</Price>
                <Price currencyname="Mexican Peso" currencycode="MXN">277.5104</Price>
                <Price currencyname="Malaysian Ringgit" currencycode="MYR">85.5771</Price>
                <Price currencyname="New Zealand Dollar" currencycode="NZD">33.4201</Price>
                <Price currencyname="Philippine Peso" currencycode="PHP">1100.5247</Price>
                <Price currencyname="Russian Rouble" currencycode="RUR">622.7094</Price>
                <Price currencyname="Singapore Dollar" currencycode="SGD">36.7982</Price>
                <Price currencyname="Turkish Lira" currencycode="TRL">33910959.7548</Price>
                <Price currencyname="United States Dollar" currencycode="USD">25.399</Price>
                <Price currencyname="Vanuatu Vatu" currencycode="VUV">2476.4112</Price>
                <Price currencyname="Rand" currencycode="ZAR">170.5447</Price>

                basically everything but the root tags. How can I get the currency name in one column and the value in the other - like:
                Currency Price
                Rand 170.5447
                • 5. Re: HTTPService question
                  ntsiii Level 3
                  See the problem here?
                  <mx:DataGridColumn headerText="Currency" dataField=""/>

                  An empty string spec for the dataField and I am not surprised it goes crazy.

                  You probably want:
                  <mx:DataGridColumn headerText="Currency" dataField="@currencyname"/>
                  • 6. Re: HTTPService question
                    ntsiii Level 3
                    Also, DataGrid wants a list or collection, and you are giving it a single node.


                    And to get the text node, for the Price column, try: