Calling a simple web service from Flex using the WebService class

by Peter deHaan on April 14, 2008

in WebService

The following example shows how you can call a ColdFusion Web Service from Flex using the <mx:WebService /> tag.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/04/14/calling-a-simple-web-service-from-flex-using-the-webservice-class/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.rpc.events.ResultEvent;
            import mx.rpc.events.FaultEvent;
            import mx.utils.ObjectUtil;

            private var startTime:int;
            private var endTime:int;

            private function button_click():void {
                webService.getMonths.send();
                startTime = getTimer();
                lbl.text = "";
            }

            private function getMonths_result(evt:ResultEvent):void {
                textArea.text = ObjectUtil.toString(evt.result);
                calcTime();
            }

            private function getMonths_fault(evt:FaultEvent):void {
                Alert.show(evt.type);
                calcTime();
            }

            private function calcTime():void {
                endTime = getTimer();
                lbl.text = "total time: " + (endTime - startTime) + "ms";
            }
        ]]>
    </mx:Script>

    <mx:WebService id="webService"
            wsdl="http://www.flash-mx.com/ws/months.cfc?wsdl">
        <mx:operation name="getMonths"
                resultFormat="object"
                result="getMonths_result(event);"
                fault="getMonths_fault(event);" />
    </mx:WebService>

    <mx:ApplicationControlBar dock="true">
        <mx:Button id="button"
                label="get months from web service"
                click="button_click();" />
        <mx:Spacer width="100%" />
        <mx:Label id="lbl" />
    </mx:ApplicationControlBar>

    <mx:TextArea id="textArea"
            editable="false"
            width="100%"
            height="100%" />

</mx:Application>

View source is enabled in the following example.

If you’re using ColdFusion, the months web service is as follows:

months.cfc

<cfcomponent output="false">

    <cffunction name="getMonths" access="remote" returntype="array" output="false">
        <cfset var monthNames = ListToArray("January,February,March,April,May,June,July,August,September,October,November,December") />
        <cfreturn monthNames />
    </cffunction>

</cfcomponent>

{ 25 comments… read them below or add one }

1 RBKB April 23, 2008 at 3:27 am

I am calling a webservice using the same code as yours but it gives me following error:

RPC Fault faultString= “Unrecognized binding style ‘null’. Only
‘document’ and ‘rpc’ styles are supported.” faultCode=”Encoding Error”
faultDetail= “null”

I am not able to find out What is the reason for this error? Can you please help?

Could this be because the WSDL file I am using does not have style=”document” in . But is in the tag?

Any options to this or any other thing that I can do ?

Thanks in advance

Reply

2 RBKB April 23, 2008 at 3:29 am

In the above comment i tried to say.

The WSDL file I am using does not have style=”document” in soap:binding element. But is in operation tag. Could this be the reason why I am getting the above mentioned error?.

Reply

3 insane May 4, 2008 at 11:08 pm

question :

flex dont let me define a operation name with a dot inside, what i should i do ?
help.
thnks

Reply

4 Shameer June 27, 2008 at 12:08 am

Thanks for the code.

I tried to create a flex application, which is calling a webservice. It runs fine locally. When put on the server, I am getting an error.

The fault string looks like

message faultCode:Channel.Security.Error
faultString:’Security error accessing url’
faultDetail:’Unable to load WSDL. If currently online, please verify the URI and/or format of the WSDL (http://myshec103077d:8080/Check/addint?wsdl)

I tried putting crossdomain.xml in the root of the application server and I am able to access the cross domain file by typing
http://myshec103077d:8080/crossdomain.xml

The cross domain file which I had put is of the form

Still I am getting the same error, when I run the flex app on the server.

Can you please help me on this

Thanks,
Shameer.

Reply

5 PT July 21, 2008 at 4:06 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!

[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::invokePendingCall()[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()

Reply

6 PT July 21, 2008 at 6:56 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!

Reply

7 Akhilesh Srivastava September 23, 2008 at 2:27 am

Hi,

I am getting a query returned by the webservice. I am able to bind the returned query to gridview control. But how can I access the records returned one by one.

Eg. the query returned is name, age, salary. I want to get name in one variable, age in another and salary in third variable.

Reply

8 peterd September 23, 2008 at 7:13 am

Akhilesh Srivastava,

I believe you’d have to loop over the result set and create the variables manually if you wanted them broken down by column (name/age/salary) instead of by rows.

Peter

Reply

9 Akhilesh Srivastava September 24, 2008 at 3:28 am

Peter,

Thanks for your comment. Can you give me the code for this.

Akhil

Reply

10 gilmar September 26, 2008 at 8:07 pm

Need to proxy webservice ate proxy-config.xml

Reply

11 Jo December 7, 2008 at 9:33 pm

Nice example

Reply

12 Dudi January 7, 2009 at 9:51 am

Hi,

Is there any way I could know all avilable operations at runtime?
(means not specifiying “hardcoded” the operation name by myself)

Thanks!

Reply

13 Laurens January 27, 2009 at 7:12 am

Hi,

The example doesn’t work in flash player 10.
When I click on the button in the example above, I get an alert with “Fault” back from the webservice…

Any idea?

Laurens

Reply

14 Willem May 13, 2009 at 9:30 am

Hi,

I have the same problem as Laurens.

Can you help me?

WIllen

Reply

15 Ahmed July 20, 2009 at 3:48 am

Hi There,
is there any way to load the wsdl address from an external text or xml file?

thansk
Ahmed

Reply

16 Peter deHaan July 20, 2009 at 8:14 am

Ahmed,

Sure, I just copied my WSDL URL into an external text file, wsdlURL.txt, and then changed the <mx:WebService /> tag to the following code:

<mx:WebService id="webService">
    <mx:wsdl><mx:String source="wsdlURL.txt" /></mx:wsdl>
    <mx:operation name="getMonths"
            resultFormat="object"
            result="getMonths_result(event);"
            fault="getMonths_fault(event);" />
</mx:WebService>

Peter

Reply

17 Ahmed July 20, 2009 at 8:55 am

Thanks Peter for your help :-)

much appreciated

18 Raihaan September 8, 2009 at 6:20 am

@PT – I encountered the same error. I found that changing the soap encoding on the WSDL to version 1 seemed to fix the issue.

It would appear that when the soap encoding on the WSDL is 1.2, things go wrong.

Reply

19 Angelo September 29, 2009 at 1:35 am

Hi Peter,

I wonder if it is possible to invoke a web servic call from a TitleWindow component which I am displaying as a popup window?

Thanks.

Reply

20 Peter deHaan September 29, 2009 at 7:10 am

@Angelo,

I imagine that would work with no problems, but give it a try and let us know what you find out.

Peter

Reply

21 Angelo September 29, 2009 at 5:49 pm

Hi Peter,

I tried to do a little example but the web service doesn’t seem to get called from a popup window.

The web service is successfully called from the main application but from the popup titlewindow, it does not work.

I placed some Alert message boxes before and after the web service is called as well as in the ResultHandler event. The alert message shows prior to calling the web service but those in the ResultHandler does not show, which leads me to conclude that the web service method is not being invoked.

I also placed some logging mechanism in the back end web service method and the web service call is logged when I invoke it from my main app, but from the popup, it does not seem to get invoked.

I wonder if this is a flex limitation. If not, I would love to hear from you on this.

Am using the SDK 3.4 by the way.

Thanks.

22 flex-webservice-java October 20, 2009 at 3:55 am
23 flex-webservice-object-binding November 2, 2009 at 1:40 am

When you make a simple webservice, the result is simple to parse and bind to a String or Number object for example.
When you make a more complex webservice that transfer or receive by parameter a complex custom object, you need to bind the data to the object stub in your client.

By using Flex Builder you can do the binding work very easy.
click on the url in the signature in order to see the simple steps for do it.

Reply

24 mal November 9, 2009 at 1:15 am

@ Shameer

Even i have the same problem as yours. Is your isssue solved? If so,please help me out.

Reply

25 Boyd Mills November 13, 2009 at 7:29 am

in flex 3, the menu sequence: Data – Import Web Serverice (WSDL) does a great job of importing the code necessary to turn web service calls into rather straightforward subroutine calls (almost)

And it works great in the development environment.

My web services are being hosted by IIS7 on Windows Server 2008. The SWF file is also being loaded from same.

The problem is that when transferred to a target machine, not the development machine, the code generated by the above “automated” process explicitly specifies the development machine, not the target machine.

There must be a simple and proper way to tell the generated code where the web services are ( hint: same machine that loaded the swf file )
Any suggestions?

/Boyd

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: