18.3.配置和连接RemoteObject

18.3.1. 问题
我想要为一个Flex 应用配置一个RemoteObject 用来连接到ColdFusion,AMFPHP,或者Java对象来提供Flex 应用与服务的通讯。
18.3.2. 解决办法
在你的应用中创建一个RemoteObject 实例并且为你的服务设定id,让服务不仅仅可以通过URL 访问。
18.3.3. 讨论
RemoteObject 允许你定义介于你的应用和服务器上实际的类对象之间的通讯。这是和WebService 组件或者HTTPService 组件都截然不同的。RemoteObject 组件可以被用来呼叫一个已经被定义用来通讯的ColdFusion CFC 组件或者Java 类。RemoteObject 也可以被用来和开源项目例例如AMFPHP,SabreAMF 以及WebORB 定义的对象以及资源来进行通讯。

RemoteObject 可以定义如下属性。
channelSet : ChannelSet
提供访问serveric 所使用的ChanelSet。

concurrency : String
指明如何来控制同一服务多个呼叫的值

constructor : Object
类对象的引用或者一个给出的类实例的构造函数。

destination : String
服务的目的地

endpoint : String
让你快速指定RemoteObject 目的地的一个端点

makeObjectsBindable : Boolean
如果为true,则强制指定返回的匿名对象为可绑定对象。

operations : Object
指定服务定义的方法;使用在RemoteObject 定义在MXML 中的方法。

requestTimeout : int
提供对请求发送信息的超时限制访问,该属性单位为秒

showBusyCursor : Boolean
如果设为true,则会在服务运行时现实一个繁忙状态的鼠标光标。

source : String
你可以在客户端指定一个源地址值;该属性并不支持在swf 文件和java 对象之间使用Java适配器来坐序列化通讯的操作。

因为RemoteObject 方法可以返回一个不需要处理或者反序列化xml 的对象。RemoteObject呼叫的结果可以广播到一个ArrayCollection 对象或者ResultEvent 中的一个强类型值类型。

在如下代码片段中,一个RemoteObject 对象被配置了用来使用一个http://localhost:8400 上的一个可用的Java 服务。
+展开
-XML
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxmlwidth="400height="300">
<mx:RemoteObject id="local_serviceconcurrency="singledestination="http://localhost:8400/appshowBusyCursor="true"
source="LocalService.Namespace.Service.ServiceName">

<mx:method name="getNamesfault="getNamesFault(event)"
result="getNamesResult(event)"/>

<mx:method name="getAgesfault="getAgesFault(event)"
result="getAgesResult(event)"/>

</mx:RemoteObject>
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
private function getNamesFault(event:FaultEvent):void {
mx.controls.Alert.show(event.message as String, "Service Error");
}
private function getNamesResult(event:ResultEvent):void
{
var namesColl:ArrayCollection = event.result as ArrayCollection;
}p
private function getAgesFault(event:FaultEvent):void {
mx.controls.Alert.show(event.message as String, "Service Error");
}
private function getAgesResult(event:ResultEvent):void
{
var agesColl:ArrayCollection = event.result as ArrayCollection;
}

]]>
</mx:Script>
</mx:Application>

这个result 事件的每个方法可以被绑定到一个不同的事件处理方法上。在actionscripte 中可以通过为RemoteObject 添加一个事件监听方法来实现。
+展开
-ActionScript
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.AbstractService;
import mx.rpc.AsyncToken;
import mx.rpc.Responder;
private function init():void {
var responder:Responder = new Responder( getNamesResult, getNamesFault );
var call:AsyncToken = ( local_service as AbstractService).getNames();
call.addResponder(responder);
}
private function getNamesFault(event:FaultEvent):void {
Alert.show(event.message as String, "Service Error");
}
private function getNamesResult(event:ResultEvent):void {
var namesColl:ArrayCollection = event.result as ArrayCollection;
}

在上面的例子中, mx.rpc.Responder 类被用来存贮用来处理服务端返回的result 和fault 方法的处理函数。Responder 被添加到一个AsyncToken 类中,当服务返回成功或者失败时,将会调用到相关的方法。

加支付宝好友偷能量挖...


评论(0)网络
阅读(79)喜欢(0)flash/flex/fcs/AIR