httpservice 公共类
flex httpservice 公共类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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;
}
}
}
二、
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//封装类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>
如果你喜欢本文, 请长按二维码,关注公众号 分布式编程.
作者:分布式编程
出处:https://zthinker.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。