Using XML in Flex - Tutorial Part 4 - WebService

Flex provides an ActionScript 3.0 class named WebService and a MXML component named WebService. My references for this tutorial are the Adobe Flex 2 Language Reference, Programming ActionScript 3.0, and the Flex 2 Developer's Guide.

The WebService class and component can be used to access SOAP-based web services. For more information on SOAP-based web services see: http://www.w3schools.com/soap/default.asp.

WebService Class

Let's examine the ActionScript 3.0 WebService class first and provide an example of how to use it. The WebService class exposes several properties and methods. One of the more important properties is wsdl, which is the location of the Web Service Description Language (WSDL) document for the web service. The WSDL document details the services provided by the web service, what inputs are required to use the service, and what outputs the service returns. A detailed description of WSDL is here: http://www.w3.org/TR/wsdl.

My example for this tutorial uses the USZip WebService provided by www.webservicex.net

To use the WebService class you will need this information:

  1. WSDL - for example http://www.webservicex.net/uszip.asmx?wsdl
  2. Method Name - for example GetInfoByCity
  3. Method Parameters - for example USCity
  4. NameSpace (if you want results formatted in e4x XML) - for example http://www.webserviceX.NET (see page 1150 in Flex 2.0 Developer's Guide)

If you examine the WSDL and the SOAP envelope returned you can find the information needed above. The USZip webservice has four methods we can use. In my example I use the GetInfoByCity method. You provide this method the name of a US city and it returns an XML document with the city, state, zip, area code, and time zone. If you examine the complete results returned, you will see that the response specifies an XML namespace of http://www.webserviceX.NET. To properly use the results formatted in e4x XML, you will need to create a namespace object and use it in your Flex app (see page 1150 in the Flex 2.0 Developer's Guide).

After you've created the WebService object, you specify the values for its properties, such as WSDL and resultFormat (e4x). You also specify which functions will handle the result and fault events by using your object name.the webservice method name.addEventListener(...). For example:

WS.GetInfoByCity.addEventListener("result", resultHandler);

The above code specifies that a function named resultHandler will handle the result event for method GetInfoByCity.

One of the event handlers you specify is for the load event. The load event is triggered when the WebService has completed loading the WSDL. Inside the function you specified as handling the load event is where you can call the WebService method. You pass any arguments to the WebService's methods inside the parenthesis:

//call the WebService method GetInfoByCity
WS.GetInfoByCity(city.selectedLabel);

In the above, I'm sending to the WebService method the user's selection in the city combo box.

In the resultHandler function I refer to the XML returned by the webservice method using the dot notation I discussed in tutorial 1.

WS.GetInfoByCity.lastResult is the XML document returned

WS.GetInfoByCity.lastResult.GetInfoByCityResult.NewDataSet.Table[0].ZIP gets the value for the ZIP node that is a child of the first Table element that is a child of the NewDataSet element, that is a child of the GetInfoByCityResult element.

To view the example click here. You can right click on the example to view the source code.

mx:WebService MXML Tag

Instead of the ActionScript 3.0 WebService class you can use the WebService MXML tag. When you use the mx:WebService tag you specify the WSDL and the id value (used to refer to the component later). Inside the mx:WebService tag you use an mx:operation tag to identify the webservice method you want to use and the resultFormat for the data returned by the method. Inside the mx:operation tag you use an mx:request tag to specify the values for the method's parameters. The elements inside the mx:request tag should be named to match the parameter names specified by the webservice method.

To make the call to the webservice method, you use the id value.the method name.send(). For example:

WS.GetInfoByCity.send() (where WS is the id value for the mx:WebService component)

Once the results are returned by the webservice you can bind them to various MXML components. Again you can use the dot notation to get the value of a specific element. You can also use the XML returned to populate a DataGrid.

See the mx:WebService example and right click on it to view the source.

Please post any suggestions for improving this tutorial or questions about areas that I've not properly covered.

Related Blog Entries

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Very useful. The

WS.addEventListener("load", loadHandler);

was key for me; I'd missed this in the Flex docs.
# Posted By David | 10/25/06 9:15 PM
This example is great. The Flex Studio help was just too convoluted and short on examples. Thanks!

One thing I would add in an example is something that I am using for my project....how to send the URL of a web service, using flashvars, into the Flex project. This way, the service is not hard-coded in the project. I couldn't really get this working right using MXML, so I switched to defining the web service in ActionScript.

Basically just
private var myWebServiceURL:String;

and then in InitApp()
myWebServiceURL = Application.application.parameters.webServiceURL;

And finally in the UseWebService() method
MyService.wsdl = myWebServiceURL + "WSDL";
# Posted By Pete | 11/17/06 12:31 PM
Pete:

Good idea. I'll try that next.
# Posted By Bruce | 11/17/06 2:37 PM
I was troubled about wsdl.
But, this article solved it.
Thank you.
# Posted By Tohru | 12/21/06 6:49 PM
This is a great sample app, thanks for providing. It helped me overcome a problem with resolving the complex result back from the webservicex GetInfoByZIP service.
# Posted By Michael Brophy | 6/8/07 1:41 AM
Hi!

I'm a newbie within Flash..I'm currently working with Flash CS3...

I have some questions:

Can i use the code with Flash CS3 a well?
What alternative exists to e4x?

I have a asp.net 2.0 website...which is going to communicate/pass a variable to the action script code(eg. by java script)...which then is going to call a webservice...but how do i call the webservice method: action script code file --> (call) method in the mxml file which then call the real webservice...? how does that looks like?

Please be kind - it is my first flash app! :)

Cheers
Pablo
# Posted By Pablo | 7/17/07 4:21 PM
Pablo - I've not used CS3 so I cannot comment on it. The alternative to e4x is to use the old ActionScript 2 XML syntax.

See the area in my tutorial where I call a webservice in ActionScript.
# Posted By Bruce | 7/19/07 6:34 PM
Hi again!

I have looked at your example...made my own dot net web service, which is very simple...the method is called HelloWorld and is just returning a string (it does not take any in parameters)...and it is working very wel!! :)

This is how my returned XML looks like:

<HelloWorldResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xmlns="http://www.123.dk/"; xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">...;
<HelloWorldResult>Pablo has problems!</HelloWorldResult>
</HelloWorldResponse>

My question to you is:

How do I get/fetch the text "Pablo has problems!" from the XML??
I have tried for so many hours now and it should be VERY simple!

The DOC says: webservice.method.lastResult.Property;

Which gives:

WS.HelloWorld.lastResult.HelloWorldResult;

I really hope that you can help me on this one, i'm so close this time ;)

Best,
Pablo
# Posted By Pablo | 7/23/07 5:13 PM
Pablo - since your response includes a namespace declaration (xmlns="http://www.123.dk) you will need to declare that namespace in order to use E4X's dot notation. See the tutorial above.
# Posted By Bruce | 7/24/07 7:25 AM
i want to write the webservice in the actionscript,and use the request object to send the operation's parameters, the request is in the xml structure.and my code like this, but i find these is some mistake in the request:
var ws:WebService = new WebService();
ws.wsdl = "http://weblogs.macromedia.com/mxna/webservices/mxn...;;
ws.useProxy = false;
ws.getMostPopularPosts.addEventListener("result", doResultHandler);
ws.getMostPopularPosts.request =<request><daysBack>30</daysBack><limit>5</limit></request>*/;
ws.addEventListener("fault",faultHandler);
ws.getMostPopularPosts();   
ws.loadWSDL();
ws.getMostPopularPosts.send();
# Posted By jessica | 8/6/07 8:45 PM
Jessica - I've not used the request object property. However, the order of your code might be part of the problem. Study the source code for the ActionScript example in the above tutorial. You need to wait until the loadWSDL() method returns before requesting the webService method.
# Posted By Bruce | 8/8/07 12:23 PM
I have to pass an xml with multiple attribute to SOAP.
<soap12:body>
<method>
<someParameter attrube0="int" atribute1="string" attibutex="Boolean"/>
<otherParameter>String</otherParameter>
</method>
</soap12:body>

I can not pass someParameter as object or xml then it throwes a HTTP request error. If pass it as xmlString or String. then I get a new error back from the server.

"Error #1069: Property attrube0 not found on String and there is no default value."

I hope that there is a sollution to pass xml with attributes
# Posted By Adam Koncz | 8/14/07 9:08 AM
Hi,
As above. When I try to send an Object with SOAP I get a (Error #2032: Stream Error) error. The crossdomain.xml is ok. And i can use the webservice when I only pass single values like int or string. Is it some kind of serialization error? Is there a way to tell the SOAP how to serialize?
Cheers,
Adam
# Posted By Adam | 8/16/07 5:11 AM
Adam - I've not had to pass an Object to a SOAP web service. Try searching through the Yahoo FlexCoder's group.
# Posted By Bruce | 8/16/07 1:06 PM
hye..awesome article ..helped me alot but i have a problem....i made my webservice in dot net and it returns this when i call

myChart.getData.lastResult.toXMLString()

<getDataResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema"; xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"; xmlns="http://tempuri.org/"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">...;
<getDataResult>
<int>45</int>
<int>65</int>
<int>2</int>
</getDataResult>
</getDataResponse>

Can you pls tell me how can i bind the
<int>45</int>
<int>65</int>
<int>2</int>

to a pie chart or data Grid Single col..i have tried doing it the following way but it brings emty grid and empty chart

For data Grid
=====================================================
<mx:DataGrid dataProvider="{myChart.getData.lastResult.getDataResult}">
<mx:columns>
<mx:DataGridColumn dataField="int" headerText="Random Nos"/>
</mx:columns>
</mx:DataGrid>

For Pie Chart
====================================================
<mx:PieChart id="chart" showDataTips="true" dataProvider="{myChart.getData.lastResult.getDataResult}" >

<mx:series>
<mx:PieSeries labelPosition="callout" field="int"></mx:PieSeries>
</mx:series>
</mx:PieChart>

Pls help me on this
# Posted By helloworld | 9/4/07 12:30 AM
i have also added the namepace

private namespace webXNameSpace = "http://tempuri.org/";;
use namespace webXNameSpace;

but the above code i pasted still brings empty grid and pie chart
i have been tryin this for days now pls help me where am going wrong
# Posted By helloworld | 9/4/07 12:41 AM
HelloWorld - Try extracting from the XML returned just the int node values and using them to create an XMLListCollection and use that as the data provider.

Search my blog for XMLListCollection for an example.
# Posted By Bruce Phillips | 9/4/07 2:05 PM
i am new to flex started 2 days back :(..pls tell me why is it that we cannot directly bind the result of the service to the controls...i have tried the webservice X and it binds correctly...why is this service not binding correctly ??...

can you tell me how do i extract int nodes
# Posted By helloworld | 9/4/07 10:57 PM
helloworld - I'm sorry but I don't have time right now to trouble shoot your code. I recommend you study the Flex developer's guide (see adobe.com) for how to use XML with Flex. There are several examples in the guide of using XML as the data provider for various Flex controls.
# Posted By Bruce | 9/5/07 11:55 AM
helloworld - in the function that process the XML after it is returned by the webservice try using some trace statements to get values of individual nodes:

trace ( mychart.getDataLastResutlt.getDataResult.int[0] ) for example.

Then run your app in debug mode and see if any value is pulled out of the xml and displayed in the console.
# Posted By Bruce | 9/5/07 2:06 PM
helloworld - it may be the node name (int) is causing a problem. Try this statement

trace (mychart.getDataLastResutlt.getDataResult.child("int") );

You can use the child() method to get the all the child nodes named "int"

Also if you want to send me the webservice you're using, I'll test it when I can.
# Posted By Bruce | 9/6/07 10:14 AM
what is difference between the result format E4x and XML
# Posted By helloworld | 9/7/07 10:30 PM
# Posted By Bruce | 9/11/07 9:23 AM
Your code is really helpful. I am trying to do the same way, but using my own webservice.

The method is my webservice returns countries results in xml format and country name is CDATA type.

I am able to load countries xml data in TextArea control but not in the DataGrid control.

Does CDATA type has to do anything with datagrid not showing data ? I tried displaying just country id but that didn't solve the problem. Any help is appreciated .
# Posted By siri | 9/18/07 11:12 AM
Siri: I've not used a webservice that returns CDATA as the data provider for the grid. I've used it for a list without a problem though. I believe the element that contains the CDATA should work just fine in the grid. I would first check that the XML being returned is properly formatted by running the XML through a validation service (see w3c.org).
# Posted By Bruce | 9/18/07 12:48 PM
Thanks Bruce.

I solved the issue. The issue is with resultFormat="e4x" attribute in the operaton tag. Once i removed it, the datagrid gets loaded but not the TextArea. I am trying to solve this, so it will display in TextArea and Datagrid just like in your example.
# Posted By Siri | 9/19/07 8:28 AM
hi ,
your tutorial is really helpful.But i want to know how to send an xmlstring from flex application to a webservice which accepts it as input parameter and do some processing.
thanx. waiting for ur reply
# Posted By krishna mohan | 9/20/07 12:21 AM
Krishna - My example code shows how to send parameter values to the webservice. Perhaps you can store your XMLString in a variable and use that variable as the parameter to the webservice (similar to how I send the US City in my example).
# Posted By Bruce | 9/20/07 9:46 AM
Thanx bruce. when sending xml data as input i had a problem.iam using mxml tag to invoke a webservice.the webservice input is xmlstring.i have taken the string variable and assigned the xmldata to it and passing that string as input inside <request > tag of <operation>.The problem is < symbol in xml it is not taking as input .when i try to use &lt; its working fine.why it is happeninig so .how can i input the xml data to webservice.
# Posted By krishnamohan | 9/21/07 2:32 AM
My web service is working fine with flex. but some time s it takes more than 5 min to get the data (beacuase of the query delay).

i used tmeout varible to close web service with in 2 min. but it does not release channels. total browser struked for ever.

i am unable to do new request unless i close and reopen the browser.
# Posted By pradeep | 10/2/07 1:17 PM
Hello Bruce,

I am having problem when I use the .swf file in my .aspx file. When I am use your example (webServiceExample.html's .swf file in .aspx file and refer in <InvalidTag> tag, it works fine. But with my .swf file, it loads .swf file and once user make selections and click "OK" button, it just hangs up, and i don't get any error message. I am guessing there is a security issue. Any help is appreciated.

Thanks,
Siri
# Posted By Siri | 10/11/07 1:40 PM
It is not InvalidTag, it is Object tag

Siri
# Posted By siri | 10/11/07 1:42 PM
Siri - I've no experience embedding .swf in an .aspx page.
# Posted By Bruce | 10/15/07 6:30 PM
I got it. It seems this is a security issue when we have .swf file in one domain and webservice in another. In my .mxml file in included on CreationComplete Security.loadPolicyFile("http://path to crosss domain xml file"). I have the crosss domain xml file located in my webservice folder. Here is my xml file:

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE cross-domain-policy SYSTEM
"http://www.adobe.com/xml/dtds/cross-domain-policy....;
<cross-domain-policy>
<allow-access-from domain="domainName" />
</cross-domain-policy>

domainName have to be replaced with real domain name (for example: www.test.com, OR *.test.com, OR *), although it is not a good idea to replace with just '*'.

Thanks,
Siri
# Posted By Siri | 10/16/07 10:02 AM
Bruce:
I've got an HTTPRequest object that's hitting the a dynamic page on the server that generates xml on the fly, by doing another server request that can't be helped. I find that it takes too long to return the xml, and Flex tries to parse the incomplete file, thus resulting in massive errors.
Is there a way to tell it to "wait 5 seconds" before parsing or accessing the file?
# Posted By Jeremy | 10/23/07 10:57 AM
Jeremy - I don't know how to delay the automatic running of the result handler function.
# Posted By Bruce | 10/25/07 12:38 PM
Hi,

I want to clear about the data transmission that weather it needs any serialization or any configuration for simple webservice using wsdl.
Let me know the code of webservice(.asmx file). I'm getting concept and wokring on that, but blindly. So plz explain that whats going on behind.

Thanks,
Pradeep
# Posted By PradeepKumar D | 10/31/07 7:51 PM
Bruce:
Thank you for the good examples. I am trying to use the WebServiceX - stockquote service to pull in Stock data:

http://www.webservicex.net/stockquote.asmx?wsdl

I cannot seem to populate a single text field or label with individual node results from the returned Stock XML. Would you please explain the code needed to be able to parse the results for a single returned item. The following is an example of the returned results from this webservice.

<StockQuotes>
<Stock>
<Symbol>POZN</Symbol>
<Last>9.94</Last>
<Date>11/2/2007</Date>
<Time>3:00pm</Time>
<Change>-0.17</Change>
<Open>10.11</Open>
<High>10.11</High>
<Low>9.44</Low>
<Volume>674724</Volume>
<MktCap>295.2M</MktCap>
<PreviousClose>10.11</PreviousClose>
<PercentageChange>-1.68%</PercentageChange>
<AnnRange>8.29 - 19.75</AnnRange>
<Earns>-0.353</Earns>
<P-E>N/A</P-E>
<Name>POZEN INC</Name>
</Stock>
</StockQuotes>

I just want to be able to pull out individual children of the return and display them in a text field.

i.e. <mx.Label text="wsStock.GetQuote.lastResult.Symbol" or <mx.Label text="wsStock.GetQuote.lastResult.Last" - It just comes up blank when I try this.

How could one of your examples above be modified to accommodate this situation?

I appreciate any help you can offer with this.

Trace
# Posted By Trace | 11/4/07 9:11 PM
Trace - some ideas to try
1. See if the returned XML uses a name space and if so you must set a name space (see my discussion in other blog entries about name spaces)
2. Use { } around what you trying to bind the label's text value to
3. Try using toXMLString() (see my example)
# Posted By Bruce | 11/5/07 6:41 AM
don't forget the curly braces around the variables in the text string or the data provider

IE

<mx.Label htmlText = "{xmlNode.here}" dataProvider=someHTTPRequest>
# Posted By Jeremy | 11/5/07 8:21 AM
hi , I can do examples where I can populate a datagrid by fetching data from a xml file . But How to do the reverse i.e from a editable database to xml ? say I am editing one row. now new values should replace the old values in the XML file ?
# Posted By prasanna | 1/7/08 11:56 PM
Prasanna - search through my blog for an entry I did on updating an XMLListCollection and also another one I did on editing objects displayed in a DataGrid.

If you want to permanently save the changed XML, I've not done that. When I have data that must be changed in a Flex app and then stored after the changes were made I've used a database and ColdFusion on the backend.
# Posted By Bruce | 1/8/08 5:33 AM
I have tried executing this example using a "destination" for the Webservice and putting the WebService info into the proxy-config.xml using Livecycle Data Services 2.5.1.. I on the WS.GetInfoByCity(city.label) command I get errors like "getAttributeByQName is not a function"
# Posted By Heather Patterson | 1/9/08 12:50 PM
Heather - I've not used LiveCycle Data Services so I cannot comment on your issue. I did test the original code and the webservice is working.

Bruce
# Posted By Bruce | 1/9/08 3:38 PM
Hi Bruce,

I tried your web service, it works perfectly. Thanks.
I created my own web service using Flex, the web service is served over a secure http(https). I put my flex app on a https server, however, I am not able to get access to the web service. Is it the problem of cross-domain policy as everyone said?

Here is the error I got when I run my flex app:

[RPC Fault faultString="Security error accessing url" faultCode="Channel.Security.Error" faultDetail="Unable to load WSDL. If currently online, please verify the URI and/or format of the WSDL (https://webserviceexample.asmx?WSDL)"]

Do you have a hint? Any suggestion will be greatly appreciated.
# Posted By atoz | 2/27/08 12:12 PM
Atoz - I've not used webservices over https so I cannot help much. The error you describe can be caused by no cross domain file, try accessing the webservice directly. Also see my example about the RSS feeds where I used ColdFusion to access the RSS feeds and then return the XML to Flex.
# Posted By Bruce | 2/29/08 4:11 AM
The memory leaks are incredible! Have you tried calling a service 10 times and profile the memory? Any idea why?
Thx
# Posted By brokulo | 3/12/08 5:14 AM
Brokulo - I've not test accessing a web service and memory leaks. It would be interesting to see if you get the same memory issues when compiling the SWF using Flex Builder 3 instead of Flex 2. I know memory leaks were an issue in Flex 2, but I think Adobe improved that in Flex 3.
# Posted By Bruce | 3/12/08 12:02 PM
i have seen u r blog and now i have my own doubt that
i am requesting a restircted webservice for the data and i using the resulted data to populate in my charts

the problem is my data is in the fromat
<NewDataSet>
<Categories>
<CategoryName>Accessories</CategoryName>
<Date>FY 2002</Date>
<SaleAmount xsi:type="xs:decimal" xmlns:xs="http://www.w3.org/2001/XMLSchema"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">...;
</Categories>
</NewDataSet>

now i want to parse the above recieved XML file in the format of
<NewDataSet>
<Categories>
<CategoryName>Accessories</CategoryName>
<Date>2002</Date>
<SaleAmount xsi:type="xs:decimal" xmlns:xs="http://www.w3.org/2001/XMLSchema"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">...;
</Categories>
</NewDataSet>

is it possible if so pls mail me the code
# Posted By raghavendra | 3/28/08 6:00 AM
Hi, I am trying to access a webservice using a similar code in your examples. But I am getting below error. Can you help me why is it comming? I have a eventlistner for loaded event, and when it is loaded in that method I am trying to call a method on webservice.

One doubt I have is in the WSDL document I am having, in the <soap binding> tag it does not have style attribute. But it does have it in the <operation> tag. Could this be the reason of the following error? Does flex try to see it in the <soap binding> tag?

Thanks in advance for your help.


[RPC Fault faultString="Unrecognized binding style 'null'. Only 'document' and 'rpc' styles are supported." faultCode="EncodingError" faultDetail="null"]
   at mx.rpc.soap::Operation/http://www.adobe.com/2006/flex/mx/internal::invoke...()[E:\dev\3.0.x\frameworks\projects\rpc\src\mx\rpc\soap\Operation.as:774]
   at mx.rpc.soap::Operation/send()[E:\dev\3.0.x\frameworks\projects\rpc\src\mx\rpc\soap\Operation.as:688]
   at Function/http://adobe.com/AS3/2006/builtin::apply()
   at mx.rpc.soap.mxml::Operation/send()[E:\dev\3.0.x\frameworks\projects\rpc\src\mx\rpc\soap\mxml\Operation.as:167]
   at Function/http://adobe.com/AS3/2006/builtin::apply()
   at mx.rpc::AbstractService/http://www.adobe.com/2006/actionscript/flash/proxy...()[E:\dev\3.0.x\frameworks\projects\rpc\src\mx\rpc\AbstractService.as:285]
# Posted By RBKB | 4/4/08 6:50 AM
RBKB - I'm sorry but I've not seen that error before. You may want to try posting your question on the FlexCoders Yahoo group.
# Posted By Bruce | 4/4/08 6:56 AM
Hi Bruce,

I just tried your above example on web service, and it gave me error as below:

"fault: [FaultEvent fault=[RPC Fault faultString="Security error accessing url"
faultCode="Channel.Security.Error" faultDetail="Destination: DefaultHTTP"]"

Is this error related to the new release of Flash Player 9,0,124?

thanks!
Mel
# Posted By mel liu | 6/24/08 12:17 PM
Mel:

I ran the example application in my browser with flash player version of 9.0.11.5.0 and it ran fine. After upgrading to 9.0.124 I get the same error as you. I know there were some additional security constraints added to calling web services that are in another domain in the latest flash player update so I'll have to research those.
# Posted By Bruce | 6/24/08 12:52 PM
Hello Bruce,
This is one of the best and simplest example I could found on net. Thanks for this.
Now I know how to get xml data into Flex UI. I have another requirement wherein I need to submit the data from flex UI in XML format to the server side. How can I do that? Is that I have to write code to construct a xml string, taking values from my form fields or is there any other way ? I have not found any reference on this on net. Could you please help me and let me know the possible solutions. Thanks in advance
# Posted By anjali | 7/4/08 3:33 AM
Bruce, great site. very informative and helpful. however, I can't seem to get a webservice to work using this FB3 code (where textInput is dynamic operator input passed to a WebService). I've tried many things, all to no avail.
<mx:TextInput id="requestStatus_input" text="Enter text"/>
<mx:WebService id="requestStatus_ws" wsdl="http://...ip...:2580/process/GeneralStatusProcessW...;
    fault="requestStatus_fault(event);" showBusyCursor="false">
   <mx:operation name="GeneralStatusProcessWS" resultFormat="e4x" result="requestStatus_complete(event);">
      <mx:request format="xml" xmlns:xs="http//www.w3.org/2001/XMLSchema">;
         <rootSys>
            <systemId>{requestStatus_input.text}</systemId>
         </rootSys>
      </mx:request>
   </mx:operation>
</mx:WebService>

Since I can't get the above to work, I tried it in ActionScript, but don't know how to set the (mx:)request part that contains the operator text input.
statusWebService = new WebService();
statusWebService.wsdl = settings.status.@wsdl; // getfrom file

statusWebService.addEventListener("load", requestStatus_load);
statusWebService.GeneralStatusProcessWS.resultFormat = "e4x";
statusWebService.GeneralStatusProcessWS.addEventListener("fault", requestStatus_fault);
statusWebService.GeneralStatusProcessWS.addEventListener("result", requestStatus_complete);
statusWebService.GeneralStatusProcessWS.request.format = "xml";
var requestXML:XML = <rootSys><systemId>{requestStatus_name_in}</systemId></rootSys>
statusWebService.GeneralStatusProcessWS.request = requestXML;

I've search many pages but can't find what I am after. Basically I have 2 questions. 1) What do I need to do to get the mxml working with dynamic operator input? 2) How can I pass dynamic operator input to the request in AS?

thanks in advance. Paul
# Posted By paul | 7/8/08 9:28 AM
Bruce,

I have no idea about programming and I have been fighting with Flex+WebServices for a month now; I read the livedocs, tons of blogs, followed so many tutorials, downloaded different software...and could not manage to call a service until I followed your simple instructions & code. If only I had found them earlier!!

Yet, I have a question. Your coding works fine with the service USZip. However, I have tried to develop simmilar codiing for other services hosted in the same webpage, such as http://www.webservicex.net/WCF/ServiceDetails.aspx... or http://www.webservicex.net/WCF/ServiceDetails.aspx... with no succes.

If I test the operations with the HTTP Post protocol available in webservicex.net for these services, I get a slightly different result to the one we got from USZIp.

What I mean is, in USZIP the XML which results from the query starts/ends with <NewDataSet><Table>...</Table></NewDataSet>.

On the other side, in Country Details, the structure is <string><NewDataSet><Table>...</Table></NewDataSet></String>.

I suspect that the structural differemt might be the source of my inhability to handle Country Details Service (and others).

Could you advice me on this?
# Posted By Iker | 7/9/08 7:22 AM
Hi Bruce,

Nice Example
When I run the same example from Flex3 it gives me Security Error in response of the GetCityInfo Method Call
In Flex2 it works.

Please Reply
Thanx :)
# Posted By Rahim | 7/10/08 4:00 AM
Rahim - see my previous comment about Flash security changes with the new Flash Player
# Posted By Bruce | 7/10/08 9:49 AM
Hello,

Im using flex and calling a .net webservice to fill a data grid clickin on a button but i get an error, hope you can help me! Here is my flex code, webservice is working in asp normally and returns an array of objects!

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"; layout="absolute" width="100%" height="100%">
<mx:WebService id="TestApp" wsdl="http://localhost:1574/Service%20References/Northwi...;
<mx:operation name="GetCategories">
</mx:operation>
</mx:WebService>
<mx:Button click="TestApp.GetCategories.send()" x="228" y="74"></mx:Button>
<mx:Binding source="{TestApp.GetCategories.result}" destination="dgData"/>
<mx:DataGrid id="dgData" dataProvider="{TestApp.GetCategories.result}" x="139" y="128">
<mx:columns>
<mx:DataGridColumn dataField="CustomerId" headerText="Customer ID"/>
<mx:DataGridColumn dataField="OrderId" headerText="Order ID"/>
</mx:columns>
</mx:DataGrid>
</mx:Application>


[RPC Fault faultString="Unrecognized binding style 'null'. Only 'document' and 'rpc' styles are supported." faultCode="EncodingError" faultDetail="null"]
   at mx.rpc.soap::Operation/http://www.adobe.com/2006/flex/mx/internal::invoke...()[E:\dev\3.0.x\frameworks\projects\rpc\src\mx\rpc\soap\Operation.as:774]
   at mx.rpc.soap::Operation/send()[E:\dev\3.0.x\frameworks\projects\rpc\src\mx\rpc\soap\Operation.as:688]
   at Function/http://adobe.com/AS3/2006/builtin::apply()
   at mx.rpc.soap.mxml::Operation/send()
# Posted By PT | 7/21/08 2:40 AM
Hi again,

I forgot to say that error happens with WCF webservice. I tested with ASMX using the internet samples and all ran well!
As i cant find WCF samples for Flex could you tell me if Flex doesnt support WCF or something like that? Thanks!
# Posted By PT | 7/21/08 6:54 AM
Hi Bruce,
I ran your sample application there and I see that lovely security issue... I'm having the very same issue with my Flex application. It seems to work fine with older versions of the flash player 9, but if you use the latest version I get it.. I've updated my crossdomain.xml to the latest standards and I can run the application from my local machine.. When I go to upload it to my host server and run the application I get the error. If I use a computer which has a older version of the flash player it runs fine...
I'm guessing there's something I have to do on the client side here... I see from one of your last posts you were looking into this problem.. Any updates?
# Posted By Sean Og Travers | 8/13/08 11:16 AM
hi,
i have created a webservice which returns array of strings . i a calling the webservice through flex with resultformat e4x. returned value is
<HelloWorldResponse xmlns="http://tempuri.org/"; xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xmlns:xsd="http://www.w3.org/2001/XMLSchema">;
<HelloWorldResult>
<anyType xsi:type="xsd:string">kedar</anyType>
<anyType xsi:type="xsd:string">kedar</anyType>
<anyType xsi:type="xsd:string">kedar</anyType>
<anyType xsi:type="xsd:string">kedar</anyType>
<anyType xsi:type="xsd:string">kedar</anyType>
</HelloWorldResult>
</HelloWorldResponse>
now how can i access the individual string elements .i tried eventobject.result.lastResult.Property but its not working .plz help me out
# Posted By kedar | 9/8/08 2:02 AM
Hi i have return simple programme to retreive values from (.net) using wsdl.
but im getting error : Required parameter 'intNric' not found in input arguments
can you please let me find if there is any issue in this below code. thanks

regards
ganesh

<?xml version="1.0" encoding="utf-8"?>
<mx:Application pageTitle="Web Service Testing" xmlns:mx="http://www.adobe.com/2006/mxml"; layout="absolute">
<mx:WebService id="TestApp" wsdl="http://localhost:2067/IPosRepositoryWebservice/Ser...; showBusyCursor="true">
<mx:operation name="GetPersonalDetails">

<mx:request>
<nric>12</nric>
</mx:request>

</mx:operation>
</mx:WebService>
<mx:Button label=" Click Here" click="TestApp.GetPersonalDetails.send()" x="228" y="74"></mx:Button>
<mx:Binding source="{TestApp.GetPersonalDetails.result}" destination="dgData"/>
<mx:DataGrid id="dgData" dataProvider="{TestApp.GetPersonalDetails.result}" x="139" y="128">
<mx:columns>
<mx:DataGridColumn dataField="nric" headerText="nric number"/>
<mx:DataGridColumn dataField="nationality" headerText="nationality"/>
</mx:columns>
</mx:DataGrid>
</mx:Application>
# Posted By gan | 11/6/08 7:31 AM
I had "Security error accessing url" with Flash Player 9.0.124 trying to access my web services and finally figured out the proper crossdomain.xml.

The following crossdomain.xml worked for me:

<cross-domain-policy>
<allow-access-from domain="*" secure="false"/>
<allow-access-from domain="*" to-ports="80,443"/>
<allow-http-request-headers-from domain="*" headers="*" />
</cross-domain-policy>

Whoever reads it may want to take a look at the original example:
http://wadearnold.com/blog//?p=20

crossdomain.xml must be in the root folder of the server hosting web services

I was able to access web services running a Flex client from a different domain and from a different physical server
# Posted By sector | 11/7/08 4:27 AM
Hi Bruce

Thanks for your code. Im getting value in xml formated but im not getting value in data grid. can you please help me to find the issue. thanks.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"; layout="absolute">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.soap.LoadEvent;
import mx.collections.ArrayCollection;
import mx.managers.CursorManager;
import mx.utils.ArrayUtil;



private namespace webXNameSpace = "http://localhost:2067/IPosRepositoryWebservice&quo...;;
use namespace webXNameSpace;
]]>
</mx:Script>
<!--create the WebService component xml working -->
<mx:WebService
id="WS" wsdl="http://localhost:2067/IPosRepositoryWebservice/Ser...; useProxy="false"
fault="Alert.show(event.fault.faultString), &apos;Error&apos;" showBusyCursor="true">

<!--specify the WebService method to use-->
<mx:operation name="GetPersonalDetails" resultFormat="e4x">
<!--specify the values for the method parameters-->
<mx:request>
<intNric>
{intNric.selectedItem.toString()}
</intNric>
</mx:request>
</mx:operation>
</mx:WebService>

<!--create user interface-->

<mx:Panel>

<mx:Spacer height="10"/>

<mx:Spacer height="15"/>
<mx:HBox>


<mx:Label text="Select Nric" width="101" fontSize="12"/>
<mx:ComboBox id="intNric" fontSize="12">
<mx:ArrayCollection>

<mx:String>10</mx:String>
<mx:String>12</mx:String>
<mx:String>15</mx:String>

</mx:ArrayCollection>

</mx:ComboBox>
</mx:HBox>
<mx:Spacer height="25"/>

<mx:HBox fontSize="12">


<mx:Button label="Get nric" click="WS.GetPersonalDetails.send()"/>
<mx:Label text="First nric Code: " fontSize="12">

</mx:Label>

<!--Bind the Text to the first Table element&apos;s nric node in the XML returned by the WebService-->
<mx:Text text="{WS.GetPersonalDetails.lastResult.GetPersonalDetailsResult.NewDataSet.Table1.nric}" width="253" fontSize="12"/>
</mx:HBox>
<mx:Spacer height="25"/>
<mx:Label text="Complete XML Returned: ">

</mx:Label>
<mx:HDividedBox width="800">

<!--bind the TextArea to the XML returned by the WebService-->
<mx:TextArea id="myTextArea" text="{WS.GetPersonalDetails.lastResult.toXMLString()}" width="390" height="400" fontSize="12"/>

<!--using the XML returned by the WebService populate the datagrid.
Table element has the data for each nric -->
<mx:DataGrid dataProvider="{WS.GetPersonalDetails.lastResult.GetPersonalDetailsResult.NewDataSet.Table1[0].nric}" height="400" fontSize="12">

<mx:columns>
<mx:DataGridColumn dataField="nric" headerText="nric" width="50"/>
<mx:DataGridColumn dataField="nationality" headerText="nationality" width="50"/>
<mx:DataGridColumn dataField="others" headerText="Others" width="70"/>
</mx:columns>

</mx:DataGrid>
</mx:HDividedBox>


</mx:Panel>
</mx:Application>


regards
ganesh
# Posted By gan | 11/10/08 8:43 AM
Hi Bruce

im getting error in this code . your example.

code:
<mx:DataGrid dataProvider="{WS.GetPersonalDetails.lastResult.GetPersonalDetailsResult.NewDataSet.Table1[0].nric}" height="400" fontSize="12">

error:
Data binding will not be able to detect changes when using square bracket operator.
for array, please use arraycollection.getItemat()instead
# Posted By gan | 11/10/08 8:44 AM
When I select a city it is firing a faultevent. I don't know why? can you explain the reasons for the same.
# Posted By Sneha | 3/18/10 2:50 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.1.002. Contact Blog Owner