最近学习了一下Flex + FluorineFx,其中遇到了一个问题,就是FluorineFx与Flex之间怎样传值,为了这个问题在网上找了不少资料,然后有自己试了一下,下面把我的成果分享一下。
(1)传类对象,我这里没成功,不知为啥,但是网上有很多成功示例。
(2)传DataSet ,网上说加上这句话 [DataTableType( "ServiceLibrary.Book")]就行,但是我试过之后
不可以,也有说加上[DataSetType("ServiceLibrary.Book")]的,但是我试过之后在.Net端就没数据了,更别提
Flex端了。
(3)传DataTable,成功了,关键是加上 [DataTableType( "ServiceLibrary.Book")]。
(4)传XmlDocument,成功了。
(5)传String,成功了,其实String保存的也是XML形式,跟XmlDocument差不多。
(6)传 List<>,成功了,网上也有很多人都用这种方式。
FluorineFx端的Service
namespace ServiceLibrary { /// <summary> /// Fluorine sample service. /// </summary> [RemotingService("Fluorine sample service")] public class Sample { public Sample() { } public string Echo(string text) { return "Gateway echo: " + text; } /// <summary> /// 还有问题 /// </summary> /// <returns></returns> public Book GetBook() { return new Book { ID = 2, Name = "让子弹飞一会", Author = "姜文", Price = 32 }; } /// <summary> /// 还有问题,不能传值 /// </summary> /// <returns></returns> [DataTableType("Books", "ServiceLibrary.Book")] public DataSet GetDataSet() { DataSet ds = new DataSet("DS"); DataTable dt = ds.Tables.Add("Books"); dt.Columns.Add("ID", typeof(int)); dt.Columns.Add("Name", typeof(string)); dt.Columns.Add("Author", typeof(string)); dt.Columns.Add("Price", typeof(double)); DataRow dr = dt.NewRow(); dr["ID"] = 1; dr["Name"] = "《Flex游戏开发》"; dr["Author"] = "张三"; dr["Price"] = 54.85; dt.Rows.Add(dr); dr = dt.NewRow(); dr["ID"] = 2; dr["Name"] = "《Flash游戏开发》"; dr["Author"] = "李四"; dr["Price"] = 65.50; dt.Rows.Add(dr); return ds; } public XmlDocument GetXML() { DataSet ds = new DataSet("DS"); DataTable dt = ds.Tables.Add("Books"); dt.Columns.Add("ID", typeof(int)); dt.Columns.Add("Name", typeof(string)); dt.Columns.Add("Author", typeof(string)); dt.Columns.Add("Price", typeof(double)); DataRow dr = dt.NewRow(); dr["ID"] = 1; dr["Name"] = "《Flex游戏开发》"; dr["Author"] = "张三"; dr["Price"] = 54.85; dt.Rows.Add(dr); dr = dt.NewRow(); dr["ID"] = 2; dr["Name"] = "《Flash游戏开发》"; dr["Author"] = "李四"; dr["Price"] = 65.50; dt.Rows.Add(dr); String xmlStr = "<?xml version=\"1.0\" encoding=\"utf-8\"?> \n"; xmlStr += ds.GetXml(); XmlDocument xml = new XmlDocument(); xml.LoadXml(xmlStr); return xml; } public String GetStringXML() { DataSet ds = new DataSet("DS"); DataTable dt = ds.Tables.Add("Books"); dt.Columns.Add("ID", typeof(int)); dt.Columns.Add("Name", typeof(string)); dt.Columns.Add("Author", typeof(string)); dt.Columns.Add("Price", typeof(double)); DataRow dr = dt.NewRow(); dr["ID"] = 1; dr["Name"] = "《Flex游戏开发》"; dr["Author"] = "张三"; dr["Price"] = 54.85; dt.Rows.Add(dr); dr = dt.NewRow(); dr["ID"] = 2; dr["Name"] = "《Flash游戏开发》"; dr["Author"] = "李四"; dr["Price"] = 65.50; dt.Rows.Add(dr); String xmlStr = "<?xml version=\"1.0\" encoding=\"utf-8\"?> \n"; xmlStr += ds.GetXml(); return xmlStr; } [DataTableType("ServiceLibrary.Book")] public DataTable GetDataTable() { DataTable dt = new DataTable("Books"); dt.Columns.Add("ID", typeof(int)); dt.Columns.Add("Name", typeof(string)); dt.Columns.Add("Author", typeof(string)); dt.Columns.Add("Price", typeof(double)); DataRow dr = dt.NewRow(); dr["ID"] = 1; dr["Name"] = "《Flex游戏开发》"; dr["Author"] = "张三"; dr["Price"] = 54.85; dt.Rows.Add(dr); dr = dt.NewRow(); dr["ID"] = 2; dr["Name"] = "《Flash游戏开发》"; dr["Author"] = "李四"; dr["Price"] = 65.50; dt.Rows.Add(dr); return dt; } public List<Book> GetList() { List<Book> list = new List<Book>(); Book book = new Book(); book.ID = 1; book.Name = "《Flex游戏开发》"; book.Author = "张三"; book.Price = 50; list.Add(book); book = new Book(); book.ID = 1; book.Name = "《Flash游戏开发》"; book.Author = "李四"; book.Price = 60; list.Add(book); return list; } } public class Book { public Book() { } public int ID { get; set; } public string Name { get; set; } public string Author { get; set; } public double Price { get; set; } }
Flex调用代码
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> <mx:Script> <![CDATA[ import mx.collections.ArrayCollection; import mx.controls.Alert; import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; import mx.rpc.xml.SimpleXMLDecoder; import mx.utils.ObjectUtil; import mx.utils.ArrayUtil; [Bindable] private var book:BookVO; [Bindable] private var books:ArrayCollection = null; private function resultEcho(event:ResultEvent):void { // 测试成功!已经获得数据。 trace("event.result:" + event.result); Alert.show(event.result as String, "Result"); } private function faultOperation(event:FaultEvent):void { // 如果测试失败!显示无法获得数据。 trace("event.message:" + event.message); Alert.show("无法获得数据!", "错误"); } // 没实现,返回的对象为空 private function onGetBook(event:ResultEvent):void { book = event.result as BookVO; Alert.show(event.result.ID as String, "Result"); } // 成功 关键是加上 [DataTableType("Flex.CustomerVO")] // 要在 WebService加上 [DataTableType("Flex.CustomerVO")] private function onGetDataTable(event:ResultEvent):void { //---------WebService解析方法--------- //books = event.result.Tables.Books.Rows as ArrayCollection; //bookGrid.dataProvider=this.service.GetDataTable.lastResult.Tables.Books.Rows; //---------WebService解析方法--------- books = event.result as ArrayCollection; bookGrid.dataProvider= books; } // 没实现,返回的对象为空 // 在 WebService加上 [DataTableType("Flex.CustomerVO")] 在webservice成功,在这不好使 // 在 WebService加上 [DataSetType("Flex.CustomerVO")] 在webservice 和 在 这都不好使 private function onGetDataSet(event:ResultEvent):void { //---------WebService解析方法--------- //books = this.service.GetDataSet.lastResult.diffgram.DS.Books as ArrayCollection; //books = event.result.Books.serverInfo.initialData as ArrayCollection; //---------WebService解析方法--------- books = event.result as ArrayCollection; bookGrid.dataProvider= books; } // 成功!!!!!!!!!!!!!! private function onGetList(event:ResultEvent):void { books = service.GetList.lastResult as ArrayCollection; bookGrid.dataProvider= books; } // 成功!!!!!!!!!!!!!! // Webservice端是 XmlDocument 这种格式 private function onGetXML(event:ResultEvent):void { var xmltree:XML = new XML(); xmltree = event.result as XML; books = new ArrayCollection(); books.removeAll(); for each(var xml:XML in xmltree.children()) { books.addItem(xml); } bookGrid.dataProvider= books; //把数据源绑定到列表中 } // 希望XMLDocument 转化成 ArrayCollection // 没试呢 private function convertXmlToArrayCollection( file:String ):ArrayCollection { var xml:XMLDocument = new XMLDocument( file ); var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(); var data:Object = decoder.decodeXML( xml ); var array:Array = ArrayUtil.toArray(data.list.items); return new ArrayCollection(array); } // 成功!!!!!!!!!!!!!! // Webservice端是 String 这种格式,但是String存的是 XML private function onGetStringXML(event:ResultEvent):void { /* books如果为空的话就会出现不能对空对象进行操作,所以每次使用先 New 一下*/ var xmltree:XML = new XML(event.result.toString()); books = new ArrayCollection(); books.removeAll(); for each(var xml:XML in xmltree.children()) { books.addItem(xml); } bookGrid.dataProvider= books; //把数据源绑定到列表中 } ]]> </mx:Script> <!--destination必须和WebService中的服务名一样--> <!--source是服务所在的class--> <mx:RemoteObject id="service" destination="fluorine" source="ServiceLibrary.Sample" fault="faultOperation(event)" > <mx:method name="Echo" result="resultEcho(event)"/> <mx:method name="GetBook" result="onGetBook(event)"/> <mx:method name="GetDataSet" result="onGetDataSet(event)"/> <mx:method name="GetDataTable" result="onGetDataTable(event)"/> <mx:method name="GetList" result="onGetList(event)"/> <mx:method name="GetXML" result="onGetXML(event)"/> <mx:method name="GetStringXML" result="onGetStringXML(event)"/> </mx:RemoteObject> <mx:VBox paddingLeft="10" paddingRight="10" paddingTop="10" paddingBottom="10" height="162" width="915" > <mx:HBox> <mx:TextInput id="txt" /> <mx:Button label="Echo" click="service.Echo(txt.text)" /> </mx:HBox> <mx:ControlBar height="116" width="867"> <mx:Button label="DataTable" click="service.GetDataTable()"/> <mx:Button label="Book" click="service.GetBook()"/> <mx:Button label="List" click="service.GetList()"/> <mx:Button label="XML" click="service.GetXML()"/> <mx:Button label="StringXML" click="service.GetStringXML()"/> <mx:Button label="DataSet" click="service.GetDataSet()"/> <mx:TextInput width="302" text="{book.Name+book.Author+book.Price}"/> </mx:ControlBar> </mx:VBox> <mx:DataGrid x="10" y="170" width="543" height="147" id="bookGrid" > <mx:columns> <mx:DataGridColumn headerText="编号" dataField="ID"/> <mx:DataGridColumn headerText="书名" dataField="Name"/> <mx:DataGridColumn headerText="作者" dataField="Author"/> <mx:DataGridColumn headerText="价格" dataField="Price"/> </mx:columns> </mx:DataGrid> </mx:Application>