HttpService、WebService、RemoteObject是Flex中使用的三种通信方式,由于RemoteObject对服务器端环境的要求,最常用的还是前两种通信方式。这里仅对Webservice和HttpService这两种方式做简单封转演示。
WebService:
package com.sample.Service { import mx.rpc.AbstractOperation; import mx.rpc.events.ResultEvent; import mx.rpc.soap.WebService; import mx.rpc.soap.Operation; import mx.core.mx_internal; use namespace mx_internal; public dynamic class WServiceProxy extends WebService { public function WServiceProxy(destination:String=null,rootURL:String=null) { super(destination,rootURL); } public function GetMethod(FunctionName:String,CallBack:Function=null):AbstractOperation{ var OP:AbstractOperation=this.getOperation(FunctionName); if(CallBack!=null) OP.addEventListener(ResultEvent.RESULT,CallBack); return OP; } override public function getOperation(name:String):AbstractOperation{ var op:Operation = new Operation(this, name); op.asyncRequest = asyncRequest; super.initializeOperation(op as Operation); if(op.wsdlOperation==null){//WSDL加载未完成,则使用基类提供的Operate return super.getOperation(name); } return op; } /**调用示例 ws=new WebService(); ws.wsdl="http://localhost/ws/webService1.asmx?wsdl"; ws.loadWSDL(); //方法一 无参数型 声明 OP=ws.getOperation("HelloWorld"); OP.addEventListener(ResultEvent.RESULT,ret); OP.send("a1-","B1"); //调用 //方法二 直接使用型 声明 ws.Login.addEventListener(ResultEvent.RESULT,ret1); ws.Login("a","b"); * */ } }
HttpService:
package com.sample.Service { import mx.rpc.events.ResultEvent; import mx.rpc.events.FaultEvent; public dynamic class HServiceProxy { private var comm:FwConfig = FwConfig.Instance; private var m_url:String; //远程调用地址 public function HServiceProxy(destination:String,rootURL:String=null) { if(destination.length>5 && destination.substr(0,5).toLowerCase()=="http:"){ m_url = destination; //全路径 } else{ m_url = "http://127.0.0.1"+destination; //构造url全路径 } } //获取操作方法 public function GetMethod(FunctionName:String="Default", CallBack:Function=null,ErrorCallBack:Function=null):HServiceOperate{ var OP:HServiceOperate=new HServiceOperate(m_url,FunctionName); if(CallBack!=null) OP.addEventListener(ResultEvent.RESULT,CallBack); if(ErrorCallBack!=null) OP.addEventListener(FaultEvent.FAULT,ErrorCallBack); return OP; } } }
HServiceOperate:
package com.samle.Service { import mx.collections.ArrayCollection; import mx.rpc.AsyncToken; import mx.rpc.Responder; import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; import mx.rpc.http.HTTPService; import mx.rpc.events.AbstractEvent; import flash.events.EventDispatcher; /** * Dispatched when an Operation invocation successfully returns. * @eventType mx.rpc.events.ResultEvent.RESULT */ [Event(name="result", type="mx.rpc.events.ResultEvent")] /** * Dispatched when an Operation call fails. * @eventType mx.rpc.events.FaultEvent.FAULT */ [Event(name="fault", type="mx.rpc.events.FaultEvent")] public dynamic class HServiceOperate extends EventDispatcher { private var m_url:String; //远程调用地址 private var m_paras:ArrayCollection;//远程调用参数 private var m_opName:String="Default"; //调用方法 public function HServiceOperate(url:String,FunctionName:String=null) { m_url = url; if(FunctionName!=null) m_opName = FunctionName; } //调用 public function call(paras:ArrayCollection=null):void { m_paras = paras; var url:String=m_url; var para:String = ParametersAsQueryString; if(para!=null && para.length>0) //参数 url = url+"?"+para; var httpObject:HTTPService = new HTTPService(); //实例化 httpObject.resultFormat = "text";//"xml" ;//"object" //"text";//返回格式 httpObject.url = url; var responder:Responder = new Responder(onSuccess, onError);//回调方法 var call:AsyncToken = httpObject.send(); //发送请求 call.addResponder(responder); } //调用成功 private function onSuccess(event:ResultEvent):void { dispatchRpcEvent(event); } //调用失败 private function onError(event:FaultEvent):void { dispatchRpcEvent(event); } ///参数转换为QueryString private function get ParametersAsQueryString():String { var QString:String=""; if(m_paras!=null && m_paras.length>0) { for(var i:int=0;i<m_paras.length;i++) { QString+=m_paras[i].name+"="+m_paras[i].value+"&"; } QString = QString.substr(0,QString.length-1); } if(m_opName!=null && m_opName!="Default")//添加了命令 { var op:String="op="+m_opName; if(QString.length==0){ QString=op; }else{ QString=op+"&"+QString; } } return QString; } /* * 分发事件 */ private function dispatchRpcEvent(event:AbstractEvent):void { if (!event.isDefaultPrevented()) { dispatchEvent(event); } } } }
原发布于Flex中使用HttpService和WebService方式通信(2010-11-10 14:10),现转于此!