Some favorite site feeds aggregated locally: iPhone Development RSS   Adobe Labs RSS   Macrumors RSS

Tuesday, November 27, 2007

AS2 Class: XmlConduit

Tuesday, November 27, 2007   

Here is a simple class that allows you to use POST and GET to return XML data from a URL (budget webservice). It works really well, you can just extend it by adding your own required methods, or you could set it up with one generic method that supplies the event type, etc. and you could send it an object with properties instead of specific arguments (so you could loop through and inspect them).


import mx.utils.Delegate;
import mx.events.EventDispatcher;

/**
XmlConduit Class
Written by Eric E. Dolecki
Version 0.1

A simple AS2 class that makes it easy to use GET and POST calls to
a URL (passing optional params) that results in returned XML data
(and an error should supply error-case XML back too [optional server-side]).

------------------------------------------------------
Sample Usage
------------------------------------------------------

import XmlConduit;

var myConduit:XmlConduit = new XmlConduit();
var conduitListener:Object = new Object();

myConduit.addEventListener( "loginResult", conduitListener );
myConduit.addEventListener( "genericError", conduitListener );

conduitListener.loginResult = function( evtObj:Object ):Void
{
trace( "XML returned: " + evtObj.data );
};

conduitListener.genericError = function( evtObj:Object ):Void
{
trace( "Generic error: " + evtObj.data );
};

function login( sUserName:String, sPassword:String, sType:String ):Void
{
myConduit.login( sUserName, sPassword, sType );
};

// POSTS are URL encoded by default if memory serves me correctly.

login( "User", "1234", "POST" );

*/
class XmlConduit
{
private var __owner;
private var replyXML:XML;
private var send_lv:LoadVars;

function dispatchEvent() {};
function addEventListener() {};
function removeEventListener() {};

// Change this URL to suit your needs. It can also be set with the setter method.
// Keeping URL strings in the main class here is for convenience, its still generic
// enough with the setters to be able to use this for whatever URLs you'd need.
private var sLoginURL:String = "http://someurlgoeshere.com/api/";

function XmlConduit()
{
__owner = this;
mx.events.EventDispatcher.initialize( __owner );
replyXML = new XML();
send_lv = new LoadVars();
};

//////////////////////////////////////////////////////////////////////////// Private Methods (Change these to suit needs)

private function doLogin( sUserName:String, sPassword:String, sType:String ):Void
{
replyXML = new XML();
replyXML.ignoreWhite = true;
replyXML.onLoad = Delegate.create( __owner, loginLoaded );

if( sType != "GET" && sType != "POST" )
{
sType = "POST";
}

send_lv = new LoadVars();
send_lv.username = sUserName;
send_lv.password = sPassword;

// sLoginURL could be built up from base URL (sLoginURL) plus params sent with it, etc.
send_lv.sendAndLoad( sLoginURL, replyXML, sType );
};

private function loginLoaded( success:Boolean ):Void
{
if( success )
{
dispatchEvent( {target:__owner, type:"loginResult", data:replyXML} );
} else
{
dispatchEvent( {target:__owner, type:"genericError", data:replyXML} );
}
};

//////////////////////////////////////////////////////////////////////////// Public Methods (Change these to suit needs)

/**
*
* @param sUserName
* @param sPassword
*/
public function login( sUserName:String, sPassword:String, sType:String ):Void
{
doLogin( sUserName, sPassword, sType );
};

/**
*
* @param sValue Setter for the loging URL string
*/
public function set loginURL( sValue:String ):Void
{
sLoginURL = sValue;
};

/**
*
* @return Getter to return the login URL string
*/
public function get loginURL():String
{
return sLoginURL;
};

}
I hope that some find it useful. Its a decent utility class.
 
 Return to the main page
Comments:

There are currently 1 Comments:

Blogger sean said...
“got an AS3 version?”
 
 Leave a comment