使用WebService

WebService返回String
WebServiceTest.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="wsBlogAggr.Readxml.send()">
    
<mx:Script>
        
<![CDATA[
            import mx.rpc.events.ResultEvent;    
            import mx.controls.Alert;
            
            public function wsHandle(e:ResultEvent):void
            {
                ddd.text = e.result as String;
            }
        
]]>
    
</mx:Script>
    
<mx:Label text="Label" id="ddd" fontSize="12"/>
    
<mx:WebService id="wsBlogAggr" wsdl="http://localhost:2250/flex%203.0%20asp.net%20test/WebServiceTest.asmx?wsdl" useProxy="false">
    
<mx:operation name="Readxml" result="wsHandle(event);" fault="Alert.show(event.fault.faultString,'Error')" />
    
</mx:WebService>
</mx:Application>

ActiveWebService.mxml,使用动态创建WebService组件方法调用WebService。必须调用loadwsdl方法下载ws说明文件,否则即使调用webservice的方法也是没有反应的。如果WebService方法要求提供参数,可在send()中加上,如send("新闻表",2)

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="AvitHandle()">
    
<mx:Script>
        
<![CDATA[
            import mx.rpc.events.FaultEvent;
            import mx.rpc.soap.WebService;
            import mx.rpc.events.ResultEvent;    
            import mx.controls.Alert;
            
            
public function AvitHandle():void
            {
                var web:WebService 
= new WebService();
                web.wsdl 
= "http://localhost:2250/flex%203.0%20asp.net%20test/WebServiceTest.asmx?wsdl";
                
//必须调用loadwsdl方法下载ws说明文件,否则即使调用webservice的方法也是没有反应的
                web.loadWSDL();
                
//添加成功调用侦听
                web.addEventListener(ResultEvent.RESULT,wsHandle);
                
//添加错误调用侦听
                web.addEventListener(FaultEvent.FAULT, fault);
                web.Readxml.send();
            }
            
            
//成功调用处理函数
            public function wsHandle(e:ResultEvent):void
            {
                ddd.text 
= e.result as String;
            }
            
            
//错误处理函数
            public function fault(e:FaultEvent):void
            {
                Alert.show(e.fault.faultString,
'Error')
            }
        ]]
>
    
</mx:Script>
    
<mx:Label text="Label" id="ddd" fontSize="12"/>
</mx:Application>



ServiceTest.asmx

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;

using System.Xml;


/// <summary>
/// WebServiceTest 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo 
= WsiProfiles.BasicProfile1_1)]
public class WebServiceTest : System.Web.Services.WebService {

    
public WebServiceTest () {

        
//如果使用设计的组件,请取消注释以下行 
        
//InitializeComponent(); 
    }

    [WebMethod]
    
public String Readxml()
    {
        
return "你还好吗?";
    }
    
}

使用WebService返回xml格式字符串,供tree组件使用。注意,tree组件要使用<node label="Mail">,属性的方法定义,而不是字段。xml可以嵌套,tree组件自动解析。
eem.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="AvitHandle()">
    
<mx:Script>
        
<![CDATA[
            import mx.rpc.events.FaultEvent;
            import mx.rpc.soap.WebService;
            import mx.rpc.events.ResultEvent;    
            import mx.controls.Alert;
            
            
public function AvitHandle():void
            {
                var web:WebService 
= new WebService();
                web.wsdl 
= "http://localhost:2250/flex%203.0%20asp.net%20test/WebServiceTest.asmx?wsdl";
                
//必须调用loadwsdl方法下载ws说明文件,否则即使调用webservice的方法也是没有反应的
                web.loadWSDL();
                
//添加成功调用侦听
                web.addEventListener(ResultEvent.RESULT,wsHandle);
                
//添加错误调用侦听
                web.addEventListener(FaultEvent.FAULT, fault);
                web.Readxml2.send();
            }
            
            
//成功调用处理函数
            public function wsHandle(e:ResultEvent):void
            {
                ddd.text 
= e.result as String;    
                var menus:XML 
= XML(e.result as String);
                var results:XMLList 
= menus.node;
                tree.dataProvider 
= results;
            }
            
            
//错误处理函数
            public function fault(e:FaultEvent):void
            {
                Alert.show(e.fault.faultString,
'Error')
            }
        ]]
>
    
</mx:Script>
    
<mx:Label text="Label" id="ddd" fontSize="12"/>
    
<mx:Tree id="tree" width="218" height="397" labelField="@label"></mx:Tree>
</mx:Application>

Webservice方法源代码
    [WebMethod]
    
public string Readxml2()
    
{
        
string str;
        str 
= "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
        str 
+= "<menus>";

        str 
+= "<node label=\"Mail\">";
        str 
+= "<node label=\"Inbox\" />";
        str 
+= "<node label=\"Calendar\" />";
        str 
+= "</node>";

        str 
+= "<node label=\"China\">";
        str 
+= "<node label=\"guangxi\" />";
        str 
+= "<node label=\"beijing\" />";
        str 
+= "</node>";

        str 
+= "</menus>";

        
return str;
    }
posted on 2008-06-28 13:15  一粒沙  阅读(640)  评论(0编辑  收藏  举报