Flex [original] Data Request (urlloader/httpservice) Tool

Source: Internet
Author: User

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 );}}}} 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.