Flex调用WebService后的数据处理

如果WebService返回的数据为DataTable或List<>类型,可以在每次读取数据的时候加入断点,然后观察最终生成数据的结构,断点最好加在ResultEvent事件内,因为这个时候才能确保从WebService获取到了数据

下面的例子分别接收从WebService返回String、DataTable、List<>类型的数据,并作相应的处理

WebService代码(.net):

[WebMethod]
public string GetString(string name)
{
    return "你好," + name;
}
[WebMethod(Description = "该方法将返回DataTable类型的数据")]
public DataTable GetDataTable()
{
    DataTable dt = new DataTable("MusicList");
    dt.Columns.Add("ID");
    dt.Columns.Add("Song");
    dt.Columns.Add("Artist");
    DataRow dr = dt.NewRow();
    dr["ID"] = 1;
    dr["Song"] = "长途伴侣";
    dr["Artist"] = "刘德华";
    dt.Rows.Add(dr);
    dr = dt.NewRow();
    dr["ID"] = 2;
    dr["Song"] = "如果我变成回忆";
    dr["Artist"] = "Tank";
    dt.Rows.Add(dr);
    return dt;
}
[WebMethod(Description = "该方法将返回List<>类型的数据")]
public List<Music> GetList()
{
    return new List<Music>
    {
        new Music
        {
             ID = 3,
             Song = "Nobody",
             Artist = "wonder girls"
        },
        new Music
        {
            ID = 4,
            Song = "吃你煮的鱼",
            Artist = "南拳妈妈"
        }
    };
}

Flex代码:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
 <mx:Script>
  <![CDATA[
  import mx.rpc.soap.mxml.Operation;
  import mx.controls.Alert;
  import mx.rpc.events.FaultEvent;
  import mx.rpc.events.ResultEvent;
  import mx.rpc.soap.WebService;
  
  internal function onResult(evt:ResultEvent):void
  {
   var funcName:String = evt.target.name;
   switch(funcName)
   {
    case "GetString":
     Alert.show(evt.result.toString());
     break;
    case "GetDataTable":
     myGrid.dataProvider = ws1.GetDataTable.lastResult.Tables.MusicList.Rows;
     break;
    case "GetList":
     myGrid.dataProvider = ws1.GetList.lastResult;
     break;
    
   }
  }
   
  internal function onFault(evt:FaultEvent):void
  {
   Alert.show(evt.fault.faultDetail.toString());
  }
  ]]>
 </mx:Script>
 
 <mx:WebService id="ws1" wsdl="http://localhost:12354/Sample0729/WebService.asmx?wsdl">
  <mx:operation name="GetString" result="onResult(event);" fault="onFault(event);">
   <mx:request>
    <name>臣本布衣</name>
   </mx:request>
  </mx:operation>
  <mx:operation name="GetDataTable" result="onResult(event);" fault="onFault(event);" />
  <mx:operation name="GetList" result="onResult(event);" fault="onFault(event);" />
 </mx:WebService>
 
 <mx:DataGrid id="myGrid" x="167.5" y="67">
  <mx:columns>
   <mx:DataGridColumn headerText="ID" dataField="ID"/>
   <mx:DataGridColumn headerText="歌曲名称" dataField="Song"/>
   <mx:DataGridColumn headerText="歌手" dataField="Artist"/>
  </mx:columns>
 </mx:DataGrid>
 
 <mx:Button label="返回字符串" click="ws1.GetString.send();" x="115" y="266"></mx:Button>
 <mx:Button label="返回DataTable" click="ws1.GetDataTable.send();" x="258" y="266"></mx:Button>
 <mx:Button label="返回List(泛型)" click="ws1.GetList.send();" x="410" y="266"></mx:Button>
</mx:Application>

 

posted @ 2010-07-15 15:29  CoderWayne  阅读(1903)  评论(0编辑  收藏  举报