< Day Day Up > |
There are three ways of connecting to Web Services in Flash MX 2004 Professional:
Flash Remoting enables us to connect to Web Services on the server-side so larger chunks of data can be retrieved; note that the AMF format is used and conversion into ActionScript 2.0 objects happens on the server side. If we decide to connect to Web Services using the client-side classes in Flash, we have the option of using the Web Services Connector component or the Web Services classes. The advantage of using the connector is it is possible to connect to a web service without writing any ActionScript 2.0 code at all using visual data binding. If you do decide to use ActionScript 2.0 with the connector, it is a bit simpler than using the Web Services classes. The Web Services classes give the developer much more control over the code because everything can be controlled in ActionScript 2.0; in addition, there are more debugging capabilities in case something goes wrong. The following sections discuss each method of connecting to Web Services in turn . Connecting to Web Services with Flash RemotingYou can use Flash Remoting to connect to a web service on the server and then pass the resulting ActionScript objects back to Flash. There are two advantages to using Flash Remoting to connect to Web Services. The first advantage is that connecting to the web service occurs on the server, so there are no security issues involved (such as requiring a policy file). The second advantage is that using Flash Remoting can be much faster than using the Web Services classes or the connector because all the data translation occurs on the server side. If we use the Web Services Connector component or the Web Services classes instead of Flash Remoting, there is a large amount of client-side processing of the XML that must occur, which can slow down performance if we are dealing with large chunks of XML. The only disadvantage of using Flash Remoting is that you must have a server-side component, which is an additional cost for J2EE and .NET. If you are using Cold Fusion MX on the server, Flash Remoting is installed by default and is no additional cost. To use Flash Remoting, we use the getService method to point to the WSDL file instead of a server-side object, and we call the server-side method as shown in the following code. Flash Remoting is covered in depth in Chapter 14, "Using Client-Side Data Integration." var songs= my_conn.getService("http://radio.tapper.net/artist.cfc?wsdl "); songs.getArtistCount(responderObject, "Beatles");
Web Services Connector ComponentThe Web Services Connector component is a quick and easy way to call Web Services from within a Flash movie, and it enables easy use of data binding. When we right-click a method of a web service from the Web Services panel, an instance of the Web Services Connector component is automatically added to the Stage. However, no component is visible when the actual SWF file is published. It is simple to add a web service to the Stage by right-clicking from the Web Services window, as shown in Figure 13.4. Figure 13.4. When a method is right-clicked, an instance of the Web Services Connector component is automatically added to the Stage.
When you click a web service method from the Web Services panel, the URL of the WSDL is automatically filled in. This can be populated by ActionScript or by the Property Inspector. ActionScript will override all other settings and can be done as follows : <webServiceConnector>.WSDLURL = "http://radio.tapper.net/artist.cfc?wsdl"; When we specify the WSDL URL for the web service, the Web Services Connector component goes out and defines (or reads) the web service WSDL and creates a data schema. From the Schema tab of the Web Services Connector component, we can modify this data by formatting, for example, a number to two decimal places, as shown in Figure 13.5. Figure 13.5. Modifying a schema for the Web Services Connector component.
The only way to have a connector actually execute is to use the trigger method. You must have this ActionScript 2.0 code in your application, even if you are using visual data binding. The syntax is as follows: <connector_instance>.trigger(); Many Web Services require parameters to be passed to them; for example, the stock service requires a company symbol. We can pass this information either through the Property Inspector or through ActionScript 2.0 code. This can be in ActionScript by specifying the connector instance name and using the params property, which must be an array. The following ActionScript 2.0 code will pass the contents of a text field to a web service as a parameter: <connector_instance>.params = new Array ("company_txt.text"); Part of the appeal of the connector components is the ability to use visual data bindings; we can bind another UI component, such as a combo box or text component, directly to the parameters of the Web Services Connector component. In addition, we can connect a web service parameter directly to a text field, as shown in Figure 13.6: Figure 13.6. Using data binding to connect the parameters of a web service to a text field.
After the web service has been called and executed, we need to access the results. If we do this with ActionScript 2.0 code, we need to use the new event listener syntax to listen for when the last byte of data has been received from the web service. The name of the event is result ; the actual results are sent back from the web service as a property called results . The syntax is shown in the following code: var resultObject:Object = new Object(); resultObject.result = function () { trace (<connector instance>.results); } <connector_instance>.addEventListener ("result", resultObject); Doing this by means of visual data binding is simple; all we have to do is bind the results property of the web service to the appropriate component. The data schema of the web service tells what type of data is being returned and we can then link that data to the right type of component. For example, the songsweb service will return a string, which could be connected to a text component. The stock service described earlier will return an Array of Objects, so it would make sense to bind the results property of that component to the dataProvider property of a list box, as shown in Figure 13.7: Figure 13.7. Binding the results of a web service that returns an Array of Objects to the dataProvider property of a list component.
If the server produces an error while connecting to the web service, the Web Services Connector component will return a status object with properties of code , faultcode , and faultstring , as shown in the following code. Status errors can be trapped and printed using the status event. var statusObject :Object = new Object(); statusObject.status = function () { trace(stat.code); trace(stat.data.faultcode); trace(stat.data.faultstring); }; <connector_instance>.addEventListener("status", statusObject); The Web Services Connector component is a powerful way to easily connect to Web Services. If we want even more flexibility and power with ActionScript 2.0, we can use the Web Services classes. Web Services ClassesThe Web Services classes are a powerful way of connecting to Web Services. The main advantage that the classes offer is that they give the developer more control of the web service process. They include a logging functionality that tells, step-by-step, exactly what is happening; this, of course, makes debugging much easier as well. When we are dealing with a complex web service, it becomes essential to be able to follow the interactions that occur. Let's explore how to connect to the song web service using the Web Services classes.
The SOAP protocol contains the data being sent between the web service itself and the Flash Player. It can be useful to actually see the raw XML that is passed back and forth between the client and server if, for example, the data translation is not occurring as expected. The raw XML will show exactly how the SOAP packets are being put together. To have your code do this, you can just reference the request and response objects in the onResult event, as shown in the following code: Obj.onResult = function(results) { trace (songResultObj.request); trace (songResultObj.response); } The raw XML will display in the output window, as shown in Figure 13.12: Figure 13.12. The raw SOAP XML data that is sent to Flash and parsed within the Player.
The DataAccess Class with Web ServicesTo create a more object-oriented way of dealing with Web Services, it makes sense to use a generalized DataAccess class. This class is ideal for class-based development, and it will enable us to easily access the results of a web service within a class. The class is effective because it encapsulates the creation of the Web Services objects, which use calls to the onResult and onFault events. We will use the DataAccess class to call the web service, send the results back to a method of our choosing, and send any errors back to a status method of our choosing. All we will have to do is pass the WSDL to the object, and we can pass functions that we want called to handle each event.
|
< Day Day Up > |