zoukankan      html  css  js  c++  java
  • 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     }

    欢迎来我的新浪博客做客: http://blog.sina.com.cn/toto0473
  • 相关阅读:
    TP实例化模型的两种方式 M() D()
    implode 函数 把数组拼接成字符串
    用array_search 数组中查找是否存在这个 值
    SVN-001
    PHP-006
    Access数据操作-02
    Access数据操作-01
    Html解析
    浏览器Chrome对WebGL支持判断
    浏览器渲染模式设置
  • 原文地址:https://www.cnblogs.com/toto0473/p/2491330.html
Copyright © 2011-2022 走看看