`
human_zx
  • 浏览: 63593 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

独立httpservice

    博客分类:
  • flex
阅读更多

     为了把mx:httpservice从mxml文件中提出来,做成一个单独as文件,在需要通讯的地方new一下,现在遇到的问题是如何在mxml文件获得,httpsercice返回的值,找了一下午,发现原来object.call()方法,我就叫它回调吧,使得所有问得到解决,不说了直接贴代码:

mxml页面代码(当然了还是贴一些主要的代码,通用的我就不贴了,我想大家都能自己添加上去):

首先顶一个自己写的as文件

 private  var http:httpSend=new httpSend();

 首先要说一下的是,必须要导入你的这个文件

初始化回调函数:他的作用就是当httpservice完成以后,把数据返回给页面

  http.callbackFunction=this.httpComplete;    //添加回调函数

 这行代码是必须的,你可以放到init()的时候,只需要一次就可以了,

说明一下,callbackFunction,是你在as文件里面定义的ojbect function,httpComplete是在mxml上面定义的函数,如下

public function httpComplete(temp:XML):void{
				
			Alert.show(temp);
              //添加你对数据的处理部门
			}

只要你在任何时候想请求的时候你需要:

http.params.oragan=e.hitData.item.id;  //钻取时的部门id
         	     http.send();     //发送http请求

 oragan就是你在后台要获取的参数

好了下面贴出完整的as文件:

package renderer
{
	
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	import flash.net.URLVariables;
	public class httpSend
	{
		 [Bindable]public var callbackFunction:Function;  //回调函数 
		 [Bindable]public var temp:XML;
		 [Bindable] public var params:URLVariables = new URLVariables();
	private var httpService:HTTPService=new HTTPService();
		public function httpSend()
		{
		super();
		}
		
		public function send():void{
			httpService.url="http://localhost:9080/qrm/Test.do";
			httpService.resultFormat="xml";
			httpService.send(params);
			httpService.addEventListener(ResultEvent.RESULT,complete);
		    httpService.addEventListener(FaultEvent.FAULT,fault);
		}
		public function fault(e:FaultEvent):void{
			Alert.show(e.toString());
		}
		public function complete(e:ResultEvent):void{
		 temp=new XML(httpService.lastResult);	
		  callbackFunction.call(this,temp);
		}
	}
}

 注意红色的部分就我我们定义的回调函数;

ps:有资料上说可以用事件的方法也能实现,等下次实现了在贴出来。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics