Provides a commonly used tool class for data request:
1: urlloader Mode
2: httpservice
Source code:
Package { Import Flash. Events. event; Import Flash. Events. ioerrorevent; Import Flash.net. urlloader; Import Flash.net. urlloaderdataformat; Import Flash.net. URLRequest; Import Flash.net. urlrequestmethod; Import Flash.net. urlvariables; Import MX. rpc. Events. faultevent; Import MX. rpc. Events. invokeevent; Import MX. rpc. Events. resultevent; Import MX. rpc. http. httpservice; Import MX. utils. stringutil; /** * <P> Data Request DeleGate </P> * this class has tow request ways: * 1. httpservice * 2. urlloader * */ Public Class Datarequestdelegate { /** * Don't init static class * */ Public Function datarequestdelegate (){ Throw New Error ("Don't init static class" );} /** * Requestremotedata use httpservice * @ Param Requrl * @ Param Parameters (default = NULL )* @ Param Onresulthandler (default = NULL )* @ Param Onfaulthandler (default = NULL )* @ Param Oninvokehandler (default = NULL )* @ Param Resultformat (default = "object ")* */ Public Static Function requestremotedata (requrl: String, parameters: Object = Null , Onresulthandler: function = Null , Onfaulthandler: function = Null , Oninvokehandler: function = Null , Resultformat: String = "object "): Void { Try {Requrl = Stringutil. Trim (requrl); var service: httpservice = New Httpservice (); service. url = Requrl; service. resultformat = Resultformat; service. Method = Urlrequestmethod. Get; If (Onresulthandler! = Null ) Service. addeventlistener (resultevent. Result, onresulthandler ); If (Onfaulthandler! = Null ) Service. addeventlistener (faultevent. Fault, onfaulthandler ); If (Oninvokehandler! = Null ) Service. addeventlistener (invokeevent. invoke, oninvokehandler); service. Send (parameters );} Catch (Error: Error) {trace ( "Httpservice Request Remote Data error:" +Error. Message );}} /** * Requestremotedata2 use urlloader * @ Param Requrl * @ Param Parameters (default = NULL )* @ Param Dataformat (default = urlloaderdataformat. Text )* @ Param Resultfun (default = NULL )* @ Param Failfun (default = NULL )* */ Public Static Function requestremotedata2 (requrl: String, parameters: Object = Null , Dataformat: String = urlloaderdataformat. Text, resultfun: function = Null , Failfun: function = Null ): Void { Try {Requrl = Stringutil. Trim (requrl ); // VaR varvals: urlvariables = new urlvariables (); For (Var ps: String in parameters ){ // Varvals [PS] = postparam [PS]; Requrl + = ("&" + PS + "=" + Parameters [PS]); // Trace ("varvals --->" + varvals [PS]); } Trace ( "XML get URL --->" + Requrl ); // Trace (urlreq. url + "? "+ (Urlreq. Data as urlvariables). tostring ()); VaR urlreq: URLRequest = New URLRequest (requrl); urlreq. Method = Urlrequestmethod. Get; // Urlreq. Data = varvals; VaR Loader: urlloader = New Urlloader (); loader. dataformat = Dataformat; loader. Load (urlreq ); If (Resultfun! =Null ) Loader. addeventlistener (event. Complete, resultfun ); If (Failfun! = Null ) Loader. addeventlistener (ioerrorevent. io_error, failfun );} Catch (Error: Error) {trace ( "Urlloader Request Remote Data error:" + Error. Message );}}}}