Consuming Web Services with Flash


Now that you see how to make a basic web service and how to test it, let's take it a step further and bring the data into Flash.

We will begin working with the XML object to bring the data in, and finally move over to using the WebServiceConnector component.

Using the XML Object

We went over the basics of the XML object back in Chapter 24, "XML and Flash." Now we are going to use it to consume the web service that we have created.

To absorb a web service with the XML object, when you load it in, use the path to the web service, followed by a slash, and then the name of the web method being called, like this:

 myXML.load("http://localhost/myWebService.asmx/myWebMethod"); 

And after it is loaded, you will have to parse the data as an XML document.

This example uses the web service we have already created to bring in the phrase "hello world" and put it in a dynamic text field on the stage.

1.

Create a new Flash document.

2.

Draw a dynamic text field on the stage, give it an instance name of myText_txt, and make sure the border setting is turned on.

3.

Create a new layer called actions.

4.

In the Actions layer, open the Actions panel, and place this code in it:

 //create the XML object var myXML_xml:XML = new XML(); //ignore white space myXML_xml.ignoreWhite = true; //create the event for when data is loaded myXML_xml.onLoad=function(success){     if(success){         myText_txt.text = this.firstChild.firstChild.nodeValue;     }else{         myText_txt.text = "An error has occurred";     } } //load the web method result myXML_xml.load("http://localhost/hello.asmx/sayHello"); 

The preceding code first creates an XML object to absorb the web service. It then sets the ignoreWhite property to true. After that, it creates the event callback for the XML object, so that when it receives data back, it will send the result to the text field. Finally, it loads the web method.

Test the movie and you will see that the phrase "hello world" has appeared in the text field.

NOTE

If you are running certain versions of the .NET framework, the preceding code may not work correctly by default. If this is the case, add this to the web.config file located on c:\:

 <webServices>             <protocols>               <add name="HttpGet"/>             </protocols> </webServices> 

Add the preceding code before the </system.web> closing tag, and then restart your computer.


That example demonstrated how to absorb a web service with the XML object, but there is a much better way to do it using Flash Remoting.




Macromedia Flash Professional 8 Unleashed
Macromedia Flash Professional 8 Unleashed
ISBN: 0672327619
EAN: 2147483647
Year: 2005
Pages: 319

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net