Flex与AmfPHP通信之如何调用

本文用于小组内讨论,仅演示如何调用AmfPHP已经写好的类。
都是从网上找到的例子,总结出这两种方法,不知道是不是最合适的调用方法,如有高手看到,请指教!

方法一:
本方法使用NetConnect+Responder的方法,网上这种例子不多,但我用起来比较顺手,先粘在这里。
本例中,在文本区域输入一个字符串,单击CallAmfPHP,即可调用,成功之后返回一个字符串,在输入的字符串前加了一个Hello。如,输入Flex,则返回HelloFlex。
ContractedBlock.gif ExpandedBlockStart.gif Code
 1<?xml version="1.0" encoding="utf-8"?>
 2<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
 3
 4    <mx:Script>
 5        <![CDATA[
 6            import mx.controls.Alert;
 7            import mx.rpc.events.FaultEvent;
 8            import mx.rpc.events.ResultEvent;
 9            import flash.net.NetConnection;
10            import flash.net.Responder;
11            
12            //定义amfphp需要用到的连接字符串
13            private var gateway_url:String = 'http://192.168.100.100/amfphp/gateway.php';
14            private var gateway_conn:NetConnection = new NetConnection();
15            
16            public function callAmfPHP():void{
17                gateway_conn.connect(gateway_url);//和amfphp建立连接
18                
19                gateway_conn.call("benya.ExGetPath.hello", new Responder(onResult, onFault), amfphptest.text);
20                //第一个参数:调用benya.ExGetPath.hello函数
21                //第二个参数:如果成功,则调用onResult函数处理,如果失败,则调用onFault处理
22                //第三个参数:调用amfphp的函数时,需要传的参数。如果有多个参数,用逗号隔开
23            }
24            
25            private function onResult(result:Object):void{
26                amfphptest.text = result.toString();//处理返回的结量
27            }
28            
29            private function onFault(fault:Object):void{
30                Alert.show('错误');//提示错误
31            }
32        ]]>
33    </mx:Script>
34
35    <mx:TextArea id="amfphptest" x="268" y="24" height="82" width="200"/>
36    <mx:Button x="321.5" y="114" label="CallAmfPHP" click="callAmfPHP()"/>
37
38</mx:Application>
本文只是用了最简单的Hello演示,马雷你需要获得fms服务器上存着的视频的路径,可以试着调用一下benya.ExGetPath.getPath,没有参数。
网上的例子大部分都是方法二,群里面问了一些高手,基本上也是方法二,现在将方法二展示给大家。

方法二:
需要先进行一些配置
右击项目名称选属性,左边点“Flex Build Path”,右面选择“Library Path”标签,展开其中的项目,确认有rpc.swc。

在左边点“Flex Compiler”,右面的Additional compiler arguments中,在后末尾加上 -services "services-config.xml"
还需要在你的Flex项目src根目录建立一个services-config.xml文件,内容如下:
ContractedBlock.gif ExpandedBlockStart.gif Code
 1<?xml version="1.0" encoding="UTF-8"?>
 2<services-config>
 3    <services>
 4        <service id="sabreamf-flashremoting-service"
 5                 class="flex.messaging.services.RemotingService"
 6                 messageTypes="flex.messaging.messages.RemotingMessage">
 7            <destination id="amfphp">
 8                <channels>
 9                    <channel ref="my-amfphp"/>
10                </channels>
11                <properties>
12                    <source>*</source>
13                </properties>
14            </destination>
15        </service>
16    </services>
17
18    <channels>
19        <channel-definition id="my-amfphp" class="mx.messaging.channels.AMFChannel">
20            <endpoint uri="http://192.168.100.100/amfphp/gateway.php" class="flex.messaging.endpoints.AMFEndpoint"/>
21        </channel-definition>
22    </channels>
23</services-config>
注意此文件的destination标签及其指向的channel-definition标签。这里面是跟据你的情况改的。

然后就可以写flex代码了,我们也实现和上面一样的功能。添加一个Hello。
ContractedBlock.gif ExpandedBlockStart.gif Code
 1<?xml version="1.0" encoding="utf-8"?>
 2<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
 3
 4    <mx:Script>
 5        <![CDATA[
 6            import mx.rpc.events.ResultEvent;
 7            import mx.rpc.events.FaultEvent;
 8            import mx.controls.Alert;
 9            
10            private function onResult(resultEvent:ResultEvent):void{
11                amfphptest.text = resultEvent.result.toString();//处理返回的结果,存储在返回事件的result属性中
12            }
13            
14            private function onFault(faultEvent:FaultEvent):void{
15                Alert.show(faultEvent.message.toString(),'调用错误');//提示调用错误
16            }
17            
18            private function onConnectFault(fault:FaultEvent):void{
19                Alert.show(fault.message.toString(),'连接错误');//提示连接错误
20            }
21        ]]>
22    </mx:Script>
23
24    <mx:RemoteObject id="callAmfPHP" destination="amfphp" source="benya.ExGetPath" fault="onConnectFault(event)">
25    <!--destination属性是services-config.xml文件中的destination标签的id
26        source属性是要调用的类,只写到类,不写方法名benya则是存储类文件的目录名称
27        fault是当出现连接错误时调用的函数-->
28        <mx:method name="hello" result="onResult(event)" fault="onFault(event)">
29        <!--name属性指定要调用的方法,类已经在RemoteObject中指定
30            result属性指定正常返回时要调用的函数
31            fault属性指定返回错误时调用的函数-->
32            <mx:arguments>
33                <arg1>{amfphptest.text}</arg1><!--参数一-->
34            </mx:arguments>
35        </mx:method>
36    </mx:RemoteObject>
37
38    <mx:TextArea id="amfphptest" x="268" y="24" height="82" width="200"/>
39    <mx:Button x="321.5" y="114" label="CallAmfPHP" click="callAmfPHP.getOperation('hello').send()"/>
40    <!--调用RemoteObject的方法:RemoteObject的id.getOperation('method的name').send()-->
41
42</mx:Application>
43
同样,马雷可以试着调用一下getPath方法,不带参数,可以返回一个fms服务器上的视频文件的路径。

从后台返回的数据格式多种多样,可以是字符串,数组对象,跟据情况,请前台在做需求分析时提出。

转载于:https://www.cnblogs.com/Cnol/archive/2009/08/26/1554134.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值