Flex中使用HttpService和WebService方式通信

  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 { publicfunction WServiceProxy(destination:String=null,rootURL:String=null) { super(destination,rootURL); } publicfunction GetMethod(FunctionName:String,CallBack:Function=null):AbstractOperation{ var OP:AbstractOperation=this.getOperation(FunctionName); if(CallBack!=null) OP.addEventListener(ResultEvent.RESULT,CallBack); return OP; } override publicfunction getOperation(name:String):AbstractOperation{ var op:Operation = new Operation(this, name); op.asyncRequest = asyncRequest; super.initializeOperation(op as Operation); if(op.wsdlOperation==null){//WSDL加载未完成,则使用基类提供的Operatereturnsuper.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 { privatevar comm:FwConfig = FwConfig.Instance; privatevar m_url:String; //远程调用地址publicfunction 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全路径 } } //获取操作方法publicfunction 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
  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 { privatevar m_url:String; //远程调用地址privatevar m_paras:ArrayCollection;//远程调用参数privatevar m_opName:String="Default"; //调用方法publicfunction HServiceOperate(url:String,FunctionName:String=null) { m_url = url; if(FunctionName!=null) m_opName = FunctionName; } //调用publicfunction 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); } //调用成功privatefunction onSuccess(event:ResultEvent):void { dispatchRpcEvent(event); } //调用失败privatefunction onError(event:FaultEvent):void { dispatchRpcEvent(event); } ///参数转换为QueryStringprivatefunction get ParametersAsQueryString():String { var QString:String=""; if(m_paras!=null && m_paras.length>0) { for(var i:int=0;ilength;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; } }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值