Monday, February 12, 2007

XmlRpc for AS 3.0 HOWTO

Since people ask (in emails, at the short-lived MT version of this blog and on flexcoders@yahoo), here is how I use it. (DISCLAIMER: as I've mentioned, I hacked this from the original when I was just starting with Flex 2, so I gutted some code before I knew what the hell I was doing, but in this state it works for me...)

I use Flex Builder. I have the contents of the zip file right in my Flex Builder project. (NOTE: This was before I read that flex should not be a part of a package name per Adobe's license; change it, willya?)

Then I choose have a simple facade like so:

package com.qbf.flex.ct {
  // Insert correct imports here, of course...

  public class Utils {
    public static function serverCall(method:String, 
                                          handler:Function): void {, 

    public static const SERVER_CALL:XmlRpcService = 
   new XmlRpcService("http://localhost:8081/", 
                        Application.application.url.indexOf("http://") == 0? null :
   null :
   // This is explained further below... 

And from everywhere else in the code I use Utils.serverCall() as follows:

var params:Array = [{value : resdefDataToServer, type: "struct"}, 
uri, server];
Utils.serverCall("doStuff", params,
        // This callback will be executed when the server 
        // call returns
 function(result:Object):void {

Notice that you only need to specify types for parameters if it's not a string. If all your parameters are string, you can just pass an array of their values to the serverCall() -- a little sugar...

Now about this QbfFakeResponse.SINGLETON business (it's not necessary, just use null in its place if you don't need it). The definition of SERVER_CALL checks whether the application URL starts with http:// -- in my case, it means that it's running "for real". If it is running in Flex Builder's debugger, the URL will start with file://, and so I will use "fake" responses to simulate server calls (this is sometimes useful for debugging, so that I don't have to run a server). This fake response class is an implementation of com.qbf.flex.util.xmlrpc.FakeResponse interface. All you need to do is implement getResponse() method and return whatever you need by checking the method name and parameters provided.


joshua said...


Just wanted to say in advance, that for sharing all your hard work on this, I am sure it is appreciated by many people.

ok, the truth is, I am new to flex, and I thought I knew a good deal of actionscript, however, I do not understand how to use this package. I have tried creating a new instance of XmlRpcService, and calling the call method with the required items. I have no idea what a "DisplayObject(Application.application)" is and for some reason, no matter what, my array is not acceptable and gives me an error (flex does).

Would it be possible to provide an example with the values filled in? Sorry I am such a n00b here.

Again, thanks for all your work on this so far.

debedb said...

Is your project big? It may be easier if you sent me your piece and I looked at it.

In this particular case, DisplayObject(Application.application)
really is used to locate the indefinite progress bar and display
it while a request is going to
the server. (This is what I needed).

What do you mean "array is not acceptable"?
Maybe your problem is that in your
code you do not include all imports? In my example, the imports should be, at least:

import com.qbf.flex.util.xmlrpc.*;
import mx.core.Application;
import flash.display.DisplayObject;

Dean said...

greetings to all.
I would first like to thank the writers of this blog by sharing information, a few years ago I read a book called Real Estate Investment costa rica in this book deal with questions like this one.

niz said...

Hello .. firstly I would like to send greetings to all readers. After this, I recognize the content so interesting about this article. For me personally I liked all the information. I would like to know of cases like this more often. In my personal experience I might mention a book called Generic Viagra in this book that I mentioned have very interesting topics, and also you have much to do with the main theme of this article.