Populating datagrid using webservice from an xml file

29 views
Skip to first unread message

mahee

unread,
Jan 28, 2010, 12:00:21 AM1/28/10
to Flex India Community
Hi Friends,

Please help me to understand how I can populate a datagrid calling a
webservice.
Please provide me some easy to understand example or code.

My target is to call an xml file using a webservice call in flex.

Regards,
Mahee

Raghunathan Mohan

unread,
Jan 28, 2010, 9:31:41 AM1/28/10
to flex_...@googlegroups.com
Hi Mahee,
                 Here i am sending you a sample code . Hope it should help you.

  <?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
import mx.controls.Alert;

           [Bindable]
           private var shippingInfo:XMLList;

       public function handleXML(event:ResultEvent):void
            {
                shippingInfo = event.result.option as XMLList;
            }

       public function handleFault(event:FaultEvent):void
       {
          Alert.show(event.fault.faultString, "Error");
       }
]]>
</mx:Script>

    <mx:HTTPService id="xmlRPC" resultFormat="e4x" url="http://www.flexmonkeys.com/F3GSE/PartII/quickstart/xmlHttpService.aspx" result="handleXML(event);" fault="handleFault(event);" useProxy="false">
   <mx:request xmlns="">
       <zipcode>{zipcode.text}</zipcode>
       <pounds>{weight_lb.text}</pounds>
   </mx:request>
</mx:HTTPService>

<mx:Label x="56" y="32" text="Zip Code" width="55" height="18" textAlign="right" fontWeight="bold"/>
<mx:Label x="56" y="58" text="Weight" width="55" height="18" textAlign="right" fontWeight="bold"/>
<mx:TextInput x="130" y="32" id="zipcode" width="160" height="22"/>
<mx:TextInput x="130" y="58" id="weight_lb" width="160" height="22"/>
<mx:Button x="130" y="95" label="Get Shipping Options" click="xmlRPC.send();" width="160" height="22"/>
<mx:DataGrid dataProvider="{shippingInfo}" x="80" y="141" width="262" height="92" id="shippingOptionsList" editable="false" enabled="true">
   <mx:columns>
       <mx:DataGridColumn headerText="Service" dataField="service" />
       <mx:DataGridColumn headerText="Price" dataField="price" />
   </mx:columns>
</mx:DataGrid>
</mx:Application>

If you want to see the return XML then add the following code to handleXML() method

Alert.show(event.result.toString());

Regards,
Raghunathan Mohan



--
You received this message because you are subscribed to the Google Groups "Flex India Community" group.
To post to this group, send email to flex_...@googlegroups.com.
To unsubscribe from this group, send email to flex_india+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/flex_india?hl=en.


mahee

unread,
Jan 28, 2010, 1:34:32 PM1/28/10
to Flex India Community

Hi Raghunath,

Thanks for your reply. I have few questions from the code -
I copied the code and executed. I entered the zip code and weight and
clicked the button. I get a http error.

Can you tell what is wrong?

Also I have quest abt the code - what does the below code does, please
explain

<mx:HTTPService id="xmlRPC" resultFormat="e4x" url="
http://www.flexmonkeys.com/F3GSE/PartII/quickstart/xmlHttpService.aspx"
result="handleXML(event);" fault="handleFault(event);"
useProxy="false">
<mx:request xmlns="">
<zipcode>{zipcode.text}</zipcode>
<pounds>{weight_lb.text}</pounds>
</mx:request>
</mx:HTTPService>

Regards,
Mahee

On Jan 28, 6:31 am, Raghunathan Mohan <raghuflexin...@gmail.com>
wrote:

> > flex_india+...@googlegroups.com<flex_india%2Bunsubscribe@googlegrou­ps.com>


> > .
> > For more options, visit this group at

> >http://groups.google.com/group/flex_india?hl=en.- Hide quoted text -
>
> - Show quoted text -

Raghunathan Mohan

unread,
Jan 29, 2010, 6:49:23 AM1/29/10
to flex_...@googlegroups.com
Hi Mahee,

I dont know wht could be the problem ... 

better try with 

Alternatively, you can use the following ColdFusion, PHP, or Java URLs:

Coding explanation for the following part

<mx:HTTPService id="xmlRPC" resultFormat="e4x" url="
http://www.flexmonkeys.com/F3GSE/PartII/quickstart/xmlHttpService.aspx"
result="handleXML(event);" fault="handleFault(event);"
useProxy="false">
   <mx:request xmlns="">
       <zipcode>{zipcode.text}</zipcode>
       <pounds>{weight_lb.text}</pounds>
   </mx:request>
</mx:HTTPService>

This above code says you are trying to use a HttpService which will use the given url. The result format will be in e4x (E4X means "ECMAScript For XML") . If the result is success then the method handleXML(event) will be triggered. If the redult is failed then handleFault(event) will be called , where you can handle the error and give a custom message to the user. the useProxy specifies whether to use the Flex proxy service. The default value is false 
inside the <mx:request> node we will give the parameter values to that services . for this particular service we are giving zipcode and pounds as parameter


        Here you can find how this code works.

      Tutorial for that Code :     http://learn.adobe.com/wiki/display/Flex/2d.+Tutorial
       Coding Explanation :      http://learn.adobe.com/wiki/display/Flex/2b.+Code+Files


Regards,
Raghunathan Mohan


To unsubscribe from this group, send email to flex_india+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages