| Register
Sunday, July 20, 2008   

Consuming Remote Web Services with Flash MX 2004

Created By  Chris Bizzell, at  11/12/2004 - 17 comments.

Click to view this author's website.

Web Services are all about communication between diverse systems over a network. A Web Service is simply a program that is accessible over a network for use by other programs. This accessibility is achieved by outputting information in standard XML format. By utilizing XML, Web Services written in one program language residing on a server can speak to other programs written in entirely different languages which reside on another server or client that may be running a completely different operating system.

A Web Service has three elements:


Web Service Description Language (WSDL)
– describes the functions available from a Web Service and the content of any messages sent from the service
SOAP – the protocol that describes the format of messages sent to and from a service
Universal Description, Description and Integration (UDDI) – the standard used by developers for publishing Web Services.

For our purposes you really only need to worry about a service's WSDL as this document will tell us what methods or functions we can call in order to retrieve information from the service.

There are countless Web Services available and for this exercise we are going to use a service from www.synapticdigital.com that outputs a listing of states and abbreviations that we can use in a Flash list component. The service URL is: http://www.synapticdigital.com/webservice/public/regions.asmx.

Go ahead and browse to this page. You will see that the service list 5 methods. The WSDL that we are going to interact with is:

http://www.synapticdigital.com/webservice/public/regions.asmx?WSDL

This service is perfect as it outputs XML in a Flash friendly format. Click on the link for this WSDL and you will see a form that enables you to post data to the service. Enter “USA” into the text field and click Invoke. You will see the following output:

http://www.synapticdigital.com/webservice/public/regions.asmx/listByCountry_lb

Notice that the XML node named list. This node is essentially an array of state names and corresponding abbreviations that we can use in Flash.

Let’s open up Flash MX 2004 and work with this service.

To begin, name layer 1 “actions” and create new layer and name it “components”.

On the component layer drag an instance of the datagrid component and name it myGrid.

Now select frame one of the actions layer and open the Actions Panel. In the panel, type the following ActionScript. The script is commented so I will not go into describing each step.

Here is the ActionScript code:

The last thing you need to do for our example to work is to import the Web Service Classes into Flash. To do this go to Window >> Other Panels >> Common Libraries >> Classes. There you will see the Web Service classes. Drag an instance onto the stage and delete the instance. The Classes you need are now in the library ready to be called by Flash at runtime.

Your application should resemble the working example here:

I hope that this article de-mystifies Web Services and enables you to utilize this powerful tool in your Flash applications.

Next time I will examine the process of using Flash Remoting to intreact with a remote Web Service. This technique enables you to overcome some of the Flash players security measures relating to retrieving remote data.

Here are a couple of site offering free Web Services:

http://www.flash-db.com/services/

http://www.xmethods.com/

Need Professional Help For Your ActionScript Project?
ActionScript.com Consulting Services provide top quality professional ActionScript consulting to businesses around the globe. If you have a professional project in need to world-class talent, tell us about your project by requesting a quote today.

Reader Comments

  1. Anthony  Replied:
    ( 11/22/2004 At 10:42 AM)

    Thanks for the guidelines- but you need to do a little copy-editing: the single inverted commas have all turned into ’ ( A-circumflex,Euro sign, Trademark sign) and there are other punctuation substitutions throughout...
    Cheers!

  2. cvb  Replied:
    ( 11/30/2004 At 11:28 PM)

    Here is a link to the Fla:
    http://www.bizwerk.net/webservice_2004/ws_20043.zip

  3. 2much4u  Replied:
    ( 12/27/2004 At 11:51 PM)

    Is possible to get the source code of the Web Service to check how do you convert the XML?
    Thanks!

  4. cvb  Replied:
    ( 1/3/2005 At 6:28 PM)

    Here is the link to the WSDL file:
    http://www.synapticdigital.com/webservice/public/regions.asmx?WSDL

  5. arpit  Replied:
    ( 1/25/2005 At 10:43 PM)

    do the servers that host the webservices that flash can invoke have to contain the crossdomain.xml file that allows flash to use the data? Not too many web services have that.

  6. Ajay Nair  Replied:
    ( 2/20/2005 At 1:02 PM)

    Simple question, but I want to put an exit button on my flash movie.. it's an exe and i got little confused with the new MX2004 script pane.. and there is only expert mode too..

  7. Johan  Replied:
    ( 3/21/2005 At 8:41 PM)

    I Consume a ws that i create in C# with this code, it return a bool value and it run so cool.
    Thanks...

  8. dsfds  Replied:
    ( 3/23/2005 At 3:01 PM)

    sadsadsad

  9. Dan  Replied:
    ( 4/17/2005 At 3:05 AM)

    I made my own web service using .NET
    Flash loads ws succesfully (I traced the "response" - I can see SOAPed XML from Flash), but your method "result.list.length" doesn't work.
    When I switch to using your web service, everything works just fine!
    I see that in your ws response there is a tag "". Is it in any connection with "result.list.length"? I don't have that tag in my SOAP output.. Can I put it there? How?
    I don't want to do XML parsing, rather your "list" method - it's simple and effective. Can you help me?
    D.

  10. Prashant Nerkar  Replied:
    ( 11/24/2005 At 2:29 AM)

    Hi
    We are using ASP.NET 2.0 and Sql server 2005 as development technology. I need to design few flash Mx files which needs to be dynamic and must populate data from SQL server 2005. I know FLash remoting must be used to connect Flash and Asp.net.
    But is there any other way so i can skip buying Flash remoting and make direct calls to ASP.NET / C# code methods from Flash Mx files.
    I can expose ASP.NET code methods using web services or Flash can access C# components directly. Is this possible without Flash remoting ? Is there any performance issues without Flash Remoting ?, let me know
    regards
    Prashant

  11. Hugo  Replied:
    ( 1/20/2006 At 5:23 PM)

    i had the same problem, any advice.
    thanks

    btw great site
    ---
    Dan Replied:
    ( 4/17/2005 At 3:05 AM)
    I made my own web service using .NET
    Flash loads ws succesfully (I traced the "response" - I can see SOAPed XML from Flash), but your method "result.list.length" doesn't work.
    When I switch to using your web service, everything works just fine!
    I see that in your ws response there is a tag "". Is it in any connection with "result.list.length"? I don't have that tag in my SOAP output.. Can I put it there? How?
    I don't want to do XML parsing, rather your "list" method - it's simple and effective. Can you help me?
    D.

  12. jacqui wechsler  Replied:
    ( 2/6/2006 At 5:49 AM)

    Remoting is best if you need to transfer large amounts of information. However webservices can be good for transferring less information....as webservices send data as xml with a lot of wrapper/extra information it can perform badly if there is a lot of data being sent.

    Also something I learnt about using .net webservices with flash is that you can not sened flash datasets. You need to send data as arrays of objects...useful to set up a struct class for your data and send objects in an array.
    Hope this helps.

  13. yrb yrb  Replied:
    ( 3/10/2006 At 7:25 PM)

    I see the XML you are parsing is "wraped" in a list element:

    -<list>
    < Region tags and childen here>
    -</list>

    I'm returning several reccords of 3 elements from a database, but the form of my xml output is this:

    <ArrayOfBrandInformation xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/">
    <BrandInformation>
    <ImageName>adidas.gif</ImageName>
    <BrandID>33</BrandID>
    <DisplayPosition>1</DisplayPosition>
    </BrandInformation>
    <BrandInformation>
    <ImageName>paulfrank.gif</ImageName>
    <BrandID>804</BrandID>
    <DisplayPosition>2</DisplayPosition>
    </BrandInformation>
    </ArrayOfBrandInformation>

    The BrandInformation tags are NOT wrapped in a single element like "list." In flash, my "result" argument does seem to hold all the information delievered by my web service (I can see all the nodes and their values in the flash debugger), but I'm having trouble accessing any information beyond the first node, i.e., imageName = "adidas.gif."

    Here's what's happening in my flash file:

    serviceCall.onResult = function(result:Object) {

    // this is correct: it returns 2
    trace("result.length = "+ result.length);

    // these 3 calls work: they return the first record
    trace(result.BrandInformation.ImageName);
    trace(result.BrandInformation.BrandID);
    trace(result.BrandInformation.DisplayPosition);

    However, I can't seem to access the second record:
    result[1].BrandInformation.ImageName is "undefined."

    Question: the result object contains all the information returned from the web service (I can see the node contents in the debugger), but I can't access information passed the first record.

    Any help would be great. Thanks.

    - yrb

  14. yrb yrb  Replied:
    ( 3/14/2006 At 5:24 PM)

    Hope this question isn't dumb, but someone responded to my question above, but ... where is the response? It didn't come to my mailbox (& my e-mail address is correct). Have I overlooked a link to where the reply is located? Thanks.

  15. yrb yrb  Replied:
    ( 3/21/2006 At 8:01 PM)

    Heads up: There appears to be I.E. & Firefox incompatiblities when it comes to web services and flash, and -- as usual -- I.E. comes out on top. My Flash-Web Service application runs flawlessly in I.E. but fails abysmally in Firefox (version 1.5). Both Browser are running flashplayer 8.0, and the application dies in Firefox in the WebService.onFault function:

    ws: faultcode = Server
    ws: faultstring = Faulty WSDL format
    ws: detail = Definitions must be the first element in a WSDL document
    ws: element = undefined
    ws: faultactor = undefined

    Sorry Firefox -- I guess you're just going to be out of the picture when it comes to some web services. What a shame.

  16. Digital Mechanic  Replied:
    ( 4/10/2006 At 9:12 PM)

    Relating to web services... I'm using a tool to generate my WSDL's that I have little control over... the WSDL's it spits out look correct but I'm not certain of what Flash can consume. There are three delivery options for the WSDL generator: RPC/Encoded, RPC/Literal and Doc/Literal. I can trace the results in Flash if using RPC/Literal or Doc/Literal, but not when generated for RPC/Encoded. Assuming that is how it should be, I'm unable to address any portion of the AS-decoded result (for example: result.pickanelement comes back as "undefined"). Using the PendingCall class I can trace the .request, the .response, and the result - but I still can't get to the values inside the returned object.

    Any ideas?

    -DM

  17. Parag Shah  Replied:
    ( 12/10/2007 At 10:12 AM)

    Hey, I am facing a weird problem. The webservice call, made from flash, works perfectly in IE. However, on one of the firfox version (2.0.0.4) it doesn't work. On an another version (2.0.0.11), it works on one machine and doesn't respond on another machine.

    Any insight?

    Thanks in advance.

Login to post your comments. If you do not have an account with us please Register.
Copyright 2005 by ActionScript, Inc.   |  Privacy Statement  |  Terms Of Use  |  ActionScript Client Extranet