对于3种连接的封装
组件代码:
package pizazz.flex4.remote{
import flash.events.Event;
import flash.events.IOErrorEvent;
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.remoting.RemoteObject;
public class Conn{
protected function connFault(event:Event):void{
var _errInfo:String = "事件目标: " + event.target + "\n";
_errInfo += "事件类型: " + event.type + "\n";
if(event is FaultEvent){
_errInfo += "错误代码: " +
(event as FaultEvent).fault.faultCode + "\n";
_errInfo += "错误信息: " +
(event as FaultEvent).fault.faultString + "\n";
_errInfo += "错误详情: " +
(event as FaultEvent).fault.faultDetail;
}
Alert.show(_errInfo, "错误信息");
}
/**
* 连接
* @param address 连接地址
* @param result 连接结果的方法
* @param config 连接对象的配置参数
* @param fault 连接异常的处理方法
* @return 连接的对象
*/
internal function conn(address:String, result:Function,
config:Object = null, fault:Function = null):Object{
return null;
}
}
}
package pizazz.flex4.remote{
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.RemoteObject;
import pizazz.flex4.utility.ParamUtil;
public class RemoteConn extends Conn{
private var _remote:RemoteObject;
public function RemoteConn(){
_remote = new RemoteObject();
}
override internal function conn(address:String, result:Function,
config:Object = null, fault:Function = null):Object{
ParamUtil.setParam(_remote, config);
_remote.destination = address;
_remote.addEventListener(ResultEvent.RESULT, result);
_remote.addEventListener(FaultEvent.FAULT,
function(event:FaultEvent):void{
if(fault != null){
fault(event);
}else{
connFault(event);
}
}
);
return _remote;
}
public static function getRemote(destination:String, result:Function = null,
config:Object = null, fault:Function = null):RemoteObject{
return new RemoteConn().conn(destination, result, config, fault)
as RemoteObject;
}
}
}
package pizazz.flex4.remote{
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;
import pizazz.flex4.utility.ParamUtil;
public class HttpConn extends Conn{
private var _http:HTTPService;
public function HttpConn(){
_http = new HTTPService();
}
override internal function conn(address:String, result:Function,
config:Object = null, fault:Function = null):Object{
ParamUtil.setParam(_http, config);
_http.url = address;
_http.addEventListener(ResultEvent.RESULT, result);
_http.addEventListener(FaultEvent.FAULT,
function(event:FaultEvent):void{
if(fault != null){
fault(event);
}else{
connFault(event);
}
}
);
return _http;
}
public static function getRemote(url:String, result:Function,
config:Object = null, fault:Function = null):HTTPService{
return new HttpConn().conn(url, result, config, fault)
as HTTPService;
}
}
}
package pizazz.flex4.remote{
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import pizazz.flex4.utility.ParamUtil;
public class UrlConn extends Conn{
private var _request:URLRequest;
public function UrlConn(){
_request = new URLRequest();
}
override internal function conn(address:String, result:Function,
config:Object = null, fault:Function = null):Object{
_request.url = address;
var _loader:URLLoader = new URLLoader();
ParamUtil.setParam(_loader, config);
_loader.addEventListener(Event.COMPLETE, result);
_loader.addEventListener(IOErrorEvent.IO_ERROR,
function(event:IOErrorEvent):void{
if(fault != null){
fault(event);
}else{
connFault(event);
}
}
);
_loader.load(_request);
return _request;
}
public static function getRemote(url:String, result:Function,
config:Object = null, fault:Function = null):URLLoader{
return new UrlConn().conn(url, result, config, fault)
as URLLoader;
}
}
}
组件执行:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955" minHeight="600">
<s:layout>
<s:HorizontalLayout />
</s:layout>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import pizazz.flex4.remote.HttpConn;
import pizazz.flex4.remote.RemoteConn;
import pizazz.flex4.remote.UrlConn;
private function remote():void{
HttpConn.getRemote("config.xml",
function(event:ResultEvent):void{
//result
}
, {"resultFormat": "e4x"}).send({"id": 0});
RemoteConn.getRemote("service",
function(event:ResultEvent):void{
//result
}
, null,
function(event:FaultEvent):void{
Alert.show("ERROR");
}
).getOperation("method").send({"id": 0});
UrlConn.getRemote("http://127.0.0.1:8080/index",
function(event:Event):void{
}
);
}
]]>
</fx:Script>
<s:Button width="100" label="连接" click="remote()" />
</s:Application>