flex httpservice 公共类
代码
package
{
import flash.events.*;
import mx.collections.ArrayCollection;
import mx.collections.IViewCursor;
import mx.core.Application;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;
import mx.utils.ArrayUtil;
public class PhotoService
{
private var service:HTTPService;
[Bindable]
public var galleries:ArrayCollection;
public function PhotoService(url:String)
{
service = new HTTPService();
service.url = url;
service.addEventListener(ResultEvent.RESULT, resultHandler);
service.send();
}
private function resultHandler(event:ResultEvent):void
{
var result:ArrayCollection = event.result.galleries.gallery is ArrayCollection
? event.result.galleries.gallery as ArrayCollection
: new ArrayCollection(ArrayUtil.toArray(event.result.galleries.gallery));
var temp:ArrayCollection = new ArrayCollection();
var cursor:IViewCursor = result.createCursor();
while (!cursor.afterLast)
{
temp.addItem(new Gallery(cursor.current)); //解析成自己想要的类
cursor.moveNext();
}
galleries = temp;
}
}
}
二、
代码
//封装类MyServiceCall
package myas
{
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;
public class MyServiceCall
{
private var hs:HTTPService;
private var f:Function;
public function MyServiceCall()
{
hs = new HTTPService;
hs.url = "data/ex.xml";
hs.addEventListener(ResultEvent.RESULT, resultHandler);
}
public function doCall(param:Function):void
{
this.f = param;
hs.send();
}
public function resultHandler(event:ResultEvent):void
{
f.call(null, event);
}
}
}
//main
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
creationComplete="init()">
<mx:Script>
<!--[CDATA[
import mx.rpc.events.ResultEvent;
import myas.MyServiceCall;
import mx.controls.Alert;
private var result:ResultEvent;
private function init():void
{
var mysc:MyServiceCall = new MyServiceCall;
//调用 相当于send
mysc.doCall(onComplete);
}
//调用完后的处理方法,此方法中得到返回结果
private function onComplete(r:ResultEvent):void
{
result = r;
}
]]-->
</mx:Script>
</mx:Application>