Flex的通信方式

方式一:flex与Webservices与简单通信

1、格式:

flex中有个<webservices>的标签能够负责flex与webservices之间交互

view plaincopy to clipboardprint?
<mx:WebService id=" WebService组件id" wsdl="WebService地址">       
    
<mx:operation name="方法名">       
    
<mx:request>     
     
     <参数名1>值1</参数名1>       
     
     <参数名2>值2</参数名2> …       
    
</mx:request>     
     
</mx:operation>       
    
</mx:WebService>    
<mx:WebService id=" WebService组件id" wsdl="WebService地址">    
 
<mx:operation name="方法名">    
 
<mx:request>  
  
     <参数名1>值1</参数名1>    
  
     <参数名2>值2</参数名2> …    
 
</mx:request>  
  
   </mx:operation>    
 
</mx:WebService>   

<mx:operation>组件表示WebService的一个方法,属性name值必须与WebService中的定义名称相同 <mx:request>标签下传递参数,需要注意的是参数的名称及顺序必须与WebService中的定义相同。

2、范例:

本实例是整理的网络上《五分钟用Flex构建WebService应用》的博客,使用的是“http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl”地址提供的天气服务数据

view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="
http://www.adobe.com/2006/mxml" layout="absolute"> 
    <mx:WebService id="webService" 
                   wsdl="
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl
                   showBusyCursor="true" 
                   result="onLoad(event)" 
                   fault="faultHandler(event)"/> 
    <mx:Script> 
        <!--[CDATA[  
            import mx.collections.ArrayCollection;  
            import mx.utils.ArrayUtil;  
            import mx.controls.Alert;  
            import mx.rpc.events.ResultEvent;  
            import mx.rpc.events.FaultEvent;  
 
            private function onLoad(event:ResultEvent):void  
            {  
                var weatherInfo:ArrayCollection=(event.result) as ArrayCollection;  
                for (var i:int=0; i < weatherInfo.length; i++)  
                {  
                    myWeatherReport.text+=weatherInfo[i] + "/n";  
                }  
            }  
 
            private function faultHandler(event:FaultEvent):void  
            {  
                Alert.show(event.fault.toString(), "WebService Error");  
            }  
        ]]--> 
    </mx:Script> 
      
    <mx:TextInput id="cityname" 
                  text="北京" x="10" y="38"/> 
    <mx:Button label="Call WebService" 
               click="webService.getWeatherbyCityName(cityname.text)" x="178" y="38"/> 
    <mx:TextArea id="myWeatherReport" 
                 height="334" 
                 width="409" 
                 editable="false" x="10" y="82"/> 
 
</mx:Application> 

 

 

 

 

方式二:这里主要介绍Webservices使用AS代码的操作方法

As代码部分
view plaincopy to clipboardprint?
[Bindable]     
public var arrayCol:ArrayCollection;  
    
public function Test():void     
{     
    var webs:WebService=new WebService();     
    webs.wsdl="http://localhost/Web/WebService1.asmx?WSDL";     
    webs.loadWSDL();     
    webs.showBusyCursor=true;     
          
    // webs.addEventListener(LoadEvent.LOAD,load);     
    webs.addEventListener(ResultEvent.RESULT,WebsResult);     
    webs.addEventListener(FaultEvent.FAULT,webFault);     
      
    webs.GetProData();  
}     
     
public function webFault(event:FaultEvent):void     
{     
    Alert.show(event.toString());     
}     
     
public  function  WebsResult(Result:ResultEvent):void     
{     
     arrayCol =Result.result as ArrayCollection;     

            [Bindable]  
            public var arrayCol:ArrayCollection;
             
            public function Test():void  
            {  
                var webs:WebService=new WebService();  
                webs.wsdl="http://localhost/Web/WebService1.asmx?WSDL";  
                webs.loadWSDL();  
                webs.showBusyCursor=true;  
                   
                // webs.addEventListener(LoadEvent.LOAD,load);  
                webs.addEventListener(ResultEvent.RESULT,WebsResult);  
                webs.addEventListener(FaultEvent.FAULT,webFault);  
               
                webs.GetProData();
            }  
              
            public function webFault(event:FaultEvent):void  
            {  
                Alert.show(event.toString());  
            }  
              
            public  function  WebsResult(Result:ResultEvent):void  
            {  
                 arrayCol =Result.result as ArrayCollection;  
            }

view plaincopy to clipboardprint?
<mx:AdvancedDataGrid id="adg1"     
                     designViewDataType="tree" editable="true"     
                     dataProvider="{arrayCol}">     
    <mx:columns>     
        <mx:AdvancedDataGridColumn headerText="CustomerID"     
                                   dataField="CustomerID"/>     
        <mx:AdvancedDataGridColumn headerText="ShipName"     
                                   editable="false"     
                                   dataField="ShipName"/>     
        <mx:AdvancedDataGridColumn headerText="Freight"     
                                   itemEditor="mx.controls.NumericStepper"       
                                   dataField="Freight"/>     
    </mx:columns>     
</mx:AdvancedDataGrid>     
<mx:Button  label="testWeb" click="Test()"></mx:Button>  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值