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

    HTTPService question

    edzillion
      Hi
      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:
      quote:


      <mx:Script>
      <![CDATA[
      [Bindable] public var goldList:XMLList;
      [Bindable] public var goldData:XML;

      private function initializeData():void
      {
      goldRequest.send();
      goldData = goldRequest.lastResult.GoldPrice;
      goldList = goldRequest.lastResult.GoldPrice.Price;
      }
      ]]>
      </mx:Script>

      <mx:HTTPService
      id="goldRequest"
      resultFormat="object"
      url=" http://dgcsc.org/goldprices.xml" />

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



      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:
      quote:


      <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>
      </GoldPrice>



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

          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.
          <mx:HTTPService
          id="fserv"
          method="GET"
          url=" http://lp008400:8080/FlexServletProject/servlet/FlexServlet"
          fault="handleFault(event)"
          result="handleResult(event)"
          resultFormat="e4x" useProxy="false">

          </mx:HTTPService>

          then add the result handler:

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


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

            <mx:HTTPService
            id="goldRequest"
            resultFormat="object"
            result="doSomething()"
            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.
              Tracy

              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
              break;
              ...
              }
              }//onResult
              • 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:

                <mx:HTTPService
                id="goldRequest"
                resultFormat="e4x"
                result="handleResult(event)"
                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"
                dataProvider="{goldData}">
                <mx:columns>
                <mx:DataGridColumn headerText="Currency" dataField=""/>
                <mx:DataGridColumn headerText="Price" dataField="Price" width="100"/>
                </mx:columns>
                </mx:DataGrid>

                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
                etc...
                • 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"/>
                  Tracy
                  • 6. Re: HTTPService question
                    ntsiii Level 3
                    Also, DataGrid wants a list or collection, and you are giving it a single node.

                    Try:
                    dataProvider="{goldData.Price}">

                    And to get the text node, for the Price column, try:
                    dataField="*"

                    Tracy