Tuesday, November 27, 2007
I hope that some find it useful. Its a decent utility class.
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;
};
}





Postings Calendar
Close

