Making Flex Behave with Web Services

So, you go through the process of creating a web service in Flex. Wow, this is going to be easy! Look at all these wizards!
You import your WSDL, and Flex generates all sorts of neat ActionScript classes for you to use. It seems almost automatic.

That is, until you try to actually do anything, and you get the following error:

TypeError: Error #1034: Type Coercion failed: cannot convert (some object) to QName

This problem had me pulling my hair out for a couple days straight. If it seems so automatic, why isn’t it working? Well, as it turns out, Flex’s SOAP encoder (the part that serializes the object into XML) currently has quite a few limitations. It works great if your web service only wants simple objects, but if you want an array or some other complex object, forget about it!

The solution: you need to override the SOAPEncoder that Flex is using for the web service.
First, create the new SOAPEncoder class. For this example, I’ve created one that just takes XML. I know what the XML is supposed to look like for the web service call, so why don’t I just create the XML myself?

	import mx.rpc.soap.SOAPEncoder;

	public class SimpleEncoder extends SOAPEncoder
		public function SimpleEncoder()

		override public function encode(value:*, name:QName = null, type:QName = null, definition:XML = null):XMLList {
			return new XMLList(value);


Now that I have my own SOAPEncoder, I can just pass the send() function straight XML! For example, let’s say you have a web service function called “addList” that takes a list of items as it’s payload. You could do the following:

var xml:String = "";
xml += "<list>\n";
xml += "\t<item>one!</item>\n";
xml += "\t<item>two!</item>\n";
xml += "\t<item>three!</item>\n";
xml += "</list>";
var o:mx.rpc.soap.mxml.Operation = service.addList;
o.encoder = new SimpleEncoder();

The beauty of this is, it doesn’t really matter what you are trying to do. Overriding the encoder means you can build the XML yourself, instead of just trusting that Flex is going to do it properly.

  1. No comments yet.

  1. No trackbacks yet.