Home  >  

Flex 101: RPC Basics

Author photo
AddThis Social Bookmark Button
When building Flex applications, it is important to understand how to get data into and out of your applications and remote procedure calls (rpc). In this post, I'll try to shed some light on HTTPService, WebService, and RemoteObject classes and their usage.

By W3C definition, a "web service" is a software system designed to support interoperable machine-to-machine interaction over a network. All three of these classes are used to support this concept.

HTTPService
The HTTPService class is a rpc class that is used to load data from any url over http. This is typically used to load text-based files (free-text, XML, CSV, etc...) that are served by a web server. They can be static files, or dynamically created, and will work with any server-side technology. You would use the HTTPService class to access restul web services (not to be confused with the web services described below).

<mx:HTTPService 
	id="myHttpService"
	url="http://myurl/myfile.xml"
	result="resultHandler(event)"
	fault="faultHandler(event)" />
You can read more about the HTTPService class online at:
http://livedocs.adobe.com/flex/3/langref/mx/rpc/http/mxml/HTTPService.html
http://livedocs.adobe.com/flex/3/html/data_access_2.html#193905

WebService
The WebService class is a class for accessing WSDL-based web services within your flex applications. The online Flex documentation sums it up nicely:

Flex applications can interact with web services that define their interfaces in a Web Services Description Language 1.1 (WSDL 1.1) document, which is available as a URL. WSDL is a standard format for describing the messages that a web service understands, the format of its responses to those messages, the protocols that the web service supports, and where to send messages. The Flex web service API generally supports Simple Object Access Protocol (SOAP) 1.1, XML Schema 1.0 (versions 1999, 2000, and 2001), and WSDL 1.1 RPC-encoded, RPC-literal, and document-literal (bare and wrapped style parameters). The two most common types of web services use remote procedure call (RPC) encoded or document-literal SOAP bindings; the terms encoded and literal indicate the type of WSDL-to-SOAP mapping that a service uses.
<mx:WebService 
	id="myWebService"
	wsdl="http://myserver/myservice.svc?wsdl'}" />
You can read more about the WebService class online at:
http://livedocs.adobe.com/flex/3/html/data_access_3.html#193910
http://livedocs.adobe.com/flex/3/langref/mx/rpc/soap/mxml/WebService.html

RemoteObject
The RemoteObject class is used to handle Flash Remoting rpc calls within Flex applications. RemoteObject services calls are conceptually the same as web services, however data is transferred using the AMF protocol, which is generally more compact and faster to parse than xml-based web services (especially with large data sets).

<mx:RemoteObject 
	id="myRemoteObject" 
	destination="myDestination"/>
You can read more about the RemoteObject class online at:
http://livedocs.adobe.com/flex/3/html/data_access_4.html#202412
http://livedocs.adobe.com/flex/3/langref/mx/rpc/remoting/mxml/RemoteObject.html

Security sandbox restrictions apply to all three of these classes, so be sure to understand security restrictions and read up on cross-domain policies.

___________________________________
Andrew Trice
Principal Architect
Cynergy Systems
http://www.cynergysystems.com

Read more from Andrew Trice. Andrew Trice's Atom feed

Comments

4 Comments

Nick Wiggill said:

I think anything that clarifies why one would pick one option as opposed to another in Flex in useful. In particular comms; I struggled with this on my initial bouts with Flex, wondering if I had picked the right option for what I was trying to achieve.

As always, "sucks to livedocs"!

Todd said:

Usually existing backends/developer skills determine which method to go with. However, if you're a sole-dev, working on a simple solution by yourself, I'd stick to using HTTPService and textual-based request/response, as it's the easiest to work with AND provides the most flexibility.

With WebService, you'll have to navigate the complex SOAP-based world and have to have your requests/responses form to predefined schemas, and your backend tools will need to output correct responses based on those schemas (not necessarily as easy as it should be). And WebService implementations by different back-end tools and providers aren't nearly as interoperable as they'd like you to believe.

With RemoteObject, you'll need a serverside service that supports AMF. Which, luckily, has been open-sourced and has an implementation in just about every server-side technology you can think of. Things are a bit trickier to debug this on the wire, however, since it's binary data. However, like Andrew mentioned, in a lot of cases the data is compact and transfers faster, and Flex even unmarshells it quickly in the Flash Player due to using some low-level, platform-optimized code.

Now, my favorite: HttpService. Transfers data back and forth across the wire in plain text. Easy to debug. No tight data contract (could be a negative, though, depending on complexity of system). You can encode data in XML, JSON, cusotm-text or whatever textual style you want -- using JSON might be necessary if you already have existing server-side code.

When I can, I start with HttpService, and then move to the others as needed. For example, in some enterprise solutions, I've been required to use WebService and ...but even that's starting to lessen these days.

Kshama said:

Hi there,
In my application, I have TileList with a image and label in each
tile. The image and label is retrieved from a xml file.However, when I run the project it fails and points to default FaultHandler function.

On further debugging, I realized that length of XML data returned from HTTPService is 0. So the httpservice is unable to hit the xml file..right ?

here is the code:



import mx.collections.ArrayCollection;
import mx.collections.XMLListCollection;
import mx.rpc.events.ResultEvent;
[Bindable]
private var itemlist :XMLList;
private var itemlistcoll : XMLListCollection
private function xmlLoaded(evt:ResultEvent):void {
itemlist = evt.result.catalog;
trace( itemlist.length());

}
]]>



Can you please throw some light on what must have gone wrong.

Also, if the dataprovider for TileList is srv.LastResult.catalog, it seems to load the label but not image ( image is replaced by a small cross ).

Thanks,
Kshama

Rodolph Stuart said:

In response to my own message, the problem was due me putting the parameters
in the wrong order :P The correct order should be;

Key
Value
TTL
Application name

For a standard 'put', for a 'put_removable' it should be;

Key
Value
Secret algorithm
Secret hash
TTL
Application name

This all became clear when I took the time to learn the basics of Python and
understand what those scripts were doing.

I can now successfully 'put' (and 'put_removable') and 'get' with no
problems!! However :( I get an error when trying to do an 'rm'. I'm fairly
confident I'm calling the method right this time as I've essentially used
the format of the Python script available. The error I'm getting is this;

Avoiding obscuring previous error by supressing error encountered while
ending request: org.apache.xmlrpc.XmlRpcClientException: Exception closing
URLConnection

Thanks.

Rodolph Stuart
Lampadas

Leave a comment


Tag Cloud

Question of the Week: Dream App

If you had an unlimited budget and unlimited resources what application would you build and why would you build it?

Answer

Latest Features

Recommended for You

@InsideRIA on Twitter

Archives

  • Or, visit our complete archive.  

About This Site

Welcome to the premiere community site for all things RIA sponsored by O'Reilly Media and Adobe Systems Incorporated.