Flex与.NET互操作(二):基于WebService的数据访问(上)

Flex提供了<mx:WebService>、<mx:HTTPService>和<mx:RemoteObject>标签来直接访问远程数据,这用于与各种不同语言环境开发提供的远程服务端数据源(如WebService)进行数据交互通信显得更加容易.

     本文以.NET平台下C#语言开发的WebService作为远程数据源,详细介绍Flex与.NET的WebService的数据通信知识点;包括连接WebService,远程调用WebService方法,给WebService方法传递参数等相关知识点。三个标签的使用方法基本上是一样,这里就以<mx:WebService>标签为例进行介绍。

     首先看看如下代码块:

1       < mx:WebService  id ="dataService"   2          wsdl ="http://localhost/FlashFlex/DataWebService.asmx?wsdl" 3          useProxy ="false" > 4           < mx:operation  name ="HelloWorld"  result ="onSuccess(event)"  fault ="onFault(event)" /> 5           < mx:operation  name ="GetBook"  fault ="onFault(event)"  result ="onObjectSuccess(event)" /> 6       </ mx:WebService >

     wsdl属性指定到要访问的WebService的wsdl地址既可,其中定义了两个操作标签(<mx:operation>),分别对应于WebService中定义的WebMethod方法。result属性标记访问WebService方法成功后的处理函数;fault则相反,指定于访问失败的处理函数。以上两个<mx:operation>对应于WebService的WebMethod方法如下:

 1       ///   <summary>  2       ///  返回字符串  3       ///   </summary>  4       ///   <returns></returns>  5      [WebMethod]  6       public   string  HelloWorld()  7      {  8           return   " Hello World " ;  9      } 10  11       ///   <summary> 12       ///  返回一个简单对象 13       ///   </summary> 14       ///   <returns></returns> 15      [WebMethod] 16       public  Book GetBook() 17      { 18           return   new  Book 19          { 20              Id  =   1 , 21              Name  =   " 三国演义 " , 22              Author  =   " 罗贯中 " , 23              Price  =   100 24          }; 25      }

     如上便是WebService方法定义和在Flex的客户端(mxml)通过<mx:WebService>标签来访问WebService的完整流程,下面我们来看看在Flex的客户端怎么去调用WebService所定义的方法:

 1  < mx:Script >  2       <! [CDATA[  3          import mx.controls.Alert;  4          import mx.rpc.events.FaultEvent;  5          import mx.rpc.events.ResultEvent;  6            7           /* *  8           * 向WebService发起请求--调用HelloWorld方法,dataService为<mx:WebService>的id  9           *  */ 10           internal  function onRequest(): void 11          { 12              dataService.HelloWorld(); 13          } 14           15           /* * 16           * 请求成功处理返回结果 17           *  */ 18           internal  function onSuccess(evt:ResultEvent): void 19          { 20              Alert.show(evt.result.toString()); 21          } 22           23           24           /* * 25           * 请求失败的处理函数 26           *  */ 27           internal  function onFault(evt:FaultEvent): void 28          { 29              Alert.show( " 访问WebService失败! " ); 30          } 31      ]] > 32  </ mx:Script >

      通过上面的调用,就可以完成一个Flex和.NET WebService的交互。当然我们在Flash/Flex的客户端调用WebService也是可以传递参数的,如下WebService的WebMethod定义:

 1       ///   <summary>  2       ///  将传递进来的参数转化为大写字符返回  3       ///   </summary>  4       ///   <param name="value"></param>  5       ///   <returns></returns>  6      [WebMethod]  7       public   string  ConvertToUpper( string  value)  8      {  9           return  value.ToUpper(); 10      }

     通过在<mx:WebService>标签下配置<mx:operation>执行该方法就可以访问了,如下:

1  < mx:operation name = " ConvertToUpper "   result = " onSuccess(event) "  fault = " onFault(event) " />
1      /* * 2       * 向WebService发起请求 3       *  */ 4       internal  function onRequest(): void 5      { 6           // dataService.HelloWorld(); 7          dataService.ConvertToUpper( " abcdefg " ); 8      }


     另外,我们还可以通过<mx:request>来传递参数,这里只需要知道<mx:request></mx:request>里的参数配置与WebService提供的WebMethod方法参数同名就OK。

     回到前面看看WebService的方法定义,其中一个方法GetBook是返回的一个Book对象,如果是返回的对象我们在Flex的客户端怎么来获取这个对象的值呢?详细见如下代码示例:

 1       internal  function onObject(): void  2      {  3          dataService.GetBook();  4      }  5        6       internal  function onObjectSuccess(evt:ResultEvent): void  7      {  8           // 直接通过事件的result属性得到返回值,然后直接访问属性便OK  9          Alert.show(evt.result.Name); 10      } 11       12       /* * 13       * 请求失败的处理函数 14       *  */ 15       internal  function onFault(evt:FaultEvent): void 16      { 17          Alert.show( " 访问WebService失败! " ); 18      }

     如上便完成了服务端的WebService返回对象到客户端的调用。

版权说明

  本文属原创文章,欢迎转载,其版权归作者和博客园共有。  

  作      者:Beniao

 文章出处: http://www.cnblogs.com/

=====================================================================================

以下是demo.mxml代码:<?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"><mx:WebService id="dataService" wsdl="http://localhost:3035/WebService1.asmx?wsdl"useProxy="false"><mx:operation name="HelloWorld" result="onSuccess(event)"fault="onFault(event)"></mx:operation><mx:operation name="GetBook" fault="onFault(event)" result="onObjectSuccess(event)"/></mx:WebService><mx:Script><![CDATA[import mx.rpc.Fault;import mx.controls.Alert;import mx.rpc.events.FaultEvent;import mx.rpc.events.ResultEvent;internal function onRequest():void{dataService.HelloWorld(); }internal function onSuccess(evt:ResultEvent):void{Alert.show(evt.result.toString());}internal function onFault(evt:FaultEvent):void{Alert.show("访问WebService失败!");}internal function onObject():void{dataService.GetBook(); }internal function onObjectSuccess(evt:ResultEvent):void{Alert.show(evt.result.Name);}]]></mx:Script><mx:Button id="btnHello" label="CALLHELLO" click="onRequest()" x="142" y="137"></mx:Button><mx:Button x="289" y="135" label="GETBOOK" fontSize="12" click="onObject()"/></mx:Application>注:前台中webService的地址与端口要根据vs2008运行时的那个地址定,并且在运行fb3中的mxml时,vs2008的这个网站要处于运行状态,webService才可以用

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值