2 Replies Latest reply on Mar 27, 2008 10:00 AM by 5ingle5hot

    HTTPService Data Conversion Issue

    5ingle5hot
      I have a REST-style web service that returns an HTML document that looks something like this:

      <a>
      <b>
      <c>2.0</c>
      <b>
      <b>
      <c>3.2</c>
      <b>
      </a>

      I can use the browser to display this document and it looks as expected. However, when I use a Flex HTTPService to obtain the document, and then display its various values, any numbers ending with ".0" are truncated/rounded. For example, the first element <c> above is displayed as "2" instead of "2.0".

      Why are numbers being converted? Shouldn't they be treated as any other text from an XML element?

      This is how I am calling the service:

      <mx:HTTPService id="myService" url="itsurl" method="GET" useProxy="false"/>

      And this is an example that shows a number being trucated/rounded:

      <mx:Label text="{myService.lastResult.a.b[0].c}"/>

      Thanks,

      Mike
        • 1. Re: HTTPService Data Conversion Issue
          ntsiii Level 3
          The default for HTTPService is resultFormat="object" This causes Flex to convert your xml into a tree of dynamic objects. This is rarely what you want. Among other things, it tries to do type conversions.

          Instead, set resultFormat="e4x". This will give you your xml un changed. It also gives you access to the e4x API which is very powerful. It includes traversal, search and fileter functionality.

          Additionally, for anything but a trivial or demo app, do not bind directly to lastResult. Instead, use a result handler function. Here is an example, untested:

          <mx:HTTPService id="myService"... result="onResult(event)" ...
          (in script)
          import mx.rpc.ResultEvent;
          [Bindable]private var _xmlResult:XML; //instance var to hold the data
          private function onResult(event:ResultEvent):void {
          _xmlResult = XML(event.result);
          trace(_xmlResult.toXMLString()); //to see exactly what you have
          ....
          }

          Note, your xml example is not valid, so I can't show an example of an e4x expression.

          Tracy
          • 2. Re: HTTPService Data Conversion Issue
            5ingle5hot Level 1
            Thanks for your response which solved my problem.

            The XML document in my original post was mistyped, and the import statement in your solution was also mistyped, so here is the final working version for posterity:

            My document:

            <a>
            <b>
            <c>2.0</c>
            </b>
            <b>
            <c>3.2</c>
            </b>
            </a>

            My service client:

            <mx:HTTPService id="myService" url="itsurl" method="GET" useProxy="false" resultFormat="e4x" result="onResult(event)"/>

            My event handling code:

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

            [Bindable]
            private var myDocument:XML;

            private function onResult(event:ResultEvent) : void {
            myDocument = XML(event.result);
            }
            ]]>
            </mx:Script>

            An example of displaying a value:

            <mx:Label text="{myDocument.b[0].c}"/>