当前位置: 代码迷 >> Web前端 >> Flex与服务器交互之三(运用WebService组件同服务器WebService通讯)
  详细解决方案

Flex与服务器交互之三(运用WebService组件同服务器WebService通讯)

热度:281   发布时间:2012-10-31 14:37:32.0
Flex与服务器交互之三(使用WebService组件同服务器WebService通讯) .

由于Flex只是一种客户端技术其本身并不能直接同数据库交互,在实际的应用开发过程中Flex提供了如URLRequest、HTTPService、RemoteObject、WebService等类以实现同服务器的通讯和数据交互,下面做一些介绍和实例解析:
?? 1、使用WebService组件同服务器交互
????? WSDL描述以供Flex WebService解析调用相关接口信息(如图):

/img/2012/10/18/1204381274.jpg

???? 使用过程中在程序中添加一个WebService组件,设置WSDL属性为WebService的WSDL地址 调用<mx:operation>节点中定义的方法,设置result和fault事件函数接收ResultEvent和FaultEvent对象获取返回数据或捕获错误
????? WebService组件官方帮助文档http://livedocs.adobe.com/flex/3/html/help.html?content=data_access_2.html
????? WebService组件相关属性和事件:http://livedocs.adobe.com/flex/3/langref/mx/rpc/soap/mxml/WebService.html

[xhtml] view plaincopyprint?
  1. ?????<mx:WebService??
  2. ??Properties??
  3. ??concurrency="multiple|single|last"??
  4. ??destination="No?default."??
  5. ??id="No?default."??
  6. ??serviceName="No?default."??
  7. ??showBusyCursor="false|true"??
  8. ??makeObjectsBindable="false|true"??
  9. ??useProxy="false|true"??
  10. ??wsdl="No?default."???????????????//请求的WSDL地址??
  11. ???Events??
  12. fault="No?default."??????//请求发生错误时触发的事件(通常在下面<operation>具体的方法中再具体定义)??
  13. result="No?default."??????//请求完成后触发的事件以处理返回结果??
  14. />??

?
? WebService调用方法节点属性和事件(可包含多个)

[xhtml] view plaincopyprint?
  1. ?<mx:operation??
  2. ??Properties??
  3. ??concurrency="multiple|single|last"??
  4. ??name=No?default,?required.????//WebService方法名?必填??
  5. ??resultFormat="object|xml|e4x"??
  6. ??makeObjectsBindable="false|true"??
  7. ?????
  8. ??Events??
  9. fault="No?default."??????//请求发生错误时触发的事件??
  10. result="No?default."??????//请求完成后触发的事件以处理返回结果??
  11. />??

?

?? 2、应用示例:(调用WebService 根据用户输入的ID 查询返回XML中用户详细信息):
?? 服务器端WebService定义:
???

[c-sharp] view plaincopyprint?
  1. [WebService(Namespace?=?"http://csdn.richardjun.net/")]??
  2. ???[WebServiceBinding(ConformsTo?=?WsiProfiles.BasicProfile1_1)]??
  3. ???[System.ComponentModel.ToolboxItem(false)]??
  4. ???//?To?allow?this?Web?Service?to?be?called?from?script,?using?ASP.NET?AJAX,?uncomment?the?following?line.? ??
  5. ???//?[System.Web.Script.Services.ScriptService] ??
  6. ???public?class?SearchUserService?:?System.Web.Services.WebService??
  7. ???{??
  8. ??
  9. ???????[WebMethod]??
  10. ???????public?String?SearchUserName(String?useID)??
  11. ???????{??
  12. ???????????XElement?element?=?XElement.Load(Server.MapPath(?@"/FlexService/Test.XML"));??
  13. ???????????IEnumerable<XElement>?uses?=?from?v?in?element.Elements().ToList()??
  14. ????????????????????????????????????????where?(String)v.Attribute("id")?==?useID??
  15. ????????????????????????????????????????select?v;??
  16. ???????????if?(uses?!=?null?&&?uses.Count()?>?0)??
  17. ???????????????return?uses.First().ToString();??
  18. ???????????else??
  19. ???????????????return?String.Empty;??
  20. ???????}??
  21. ???}??

???

?

服务器端用户详细信息Test.XML文件:
?

[xhtml] view plaincopyprint?
  1. <?xml?version="1.0"?encoding="utf-8"??>??
  2. ?<root?xmlns="http:=//www.byd.com/ChartData/XML">??
  3. ???<User?id="0">??
  4. ?????<Name>宋江</Name>??
  5. ?????<NikeName>天魁星</NikeName>??
  6. ?????<Position>梁山泊寨主</Position>??
  7. ?????<Weapon>锟吾剑</Weapon>??
  8. ???</User>??
  9. ???<User?id="1">??
  10. ?????<Name>卢俊义</Name>??
  11. ?????<NikeName>天罡星</NikeName>??
  12. ?????<Position>梁山泊副寨主</Position>??
  13. ?????<Weapon>枪、棒</Weapon>??
  14. ???</User>??
  15. ???<User?id="2">??
  16. ?????<Name>吴用</Name>??
  17. ?????<NikeName>天机星</NikeName>??
  18. ?????<Position>梁山泊军师</Position>??
  19. ?????<Weapon>铜链</Weapon>??
  20. ???</User>??
  21. ???<User?id="3">??
  22. ?????<Name>公孙胜</Name>??
  23. ?????<NikeName>天闲星</NikeName>??
  24. ?????<Position>梁山泊副军师</Position>??
  25. ?????<Weapon>松纹古定剑</Weapon>??
  26. ???</User>??
  27. ???<User?id="4">??
  28. ?????<Name>大刀关胜</Name>??
  29. ?????<NikeName>天勇星</NikeName>??
  30. ?????<Position>梁山泊马军五虎大将之首</Position>??
  31. ?????<Weapon>青龙偃月刀</Weapon>??
  32. ???</User>??
  33. ???<User?id="5">??
  34. ?????<Name>林冲</Name>??
  35. ?????<NikeName>天雄星豹子头</NikeName>??
  36. ?????<Position>梁山泊马军五虎大将之二</Position>??
  37. ?????<Weapon>丈八蛇矛</Weapon>??
  38. ?</User>??
  39. ?</root>??
  40. ?????

?

Felx代码:
????

[c-sharp] view plaincopyprint?
  1. <mx:Canvas?x="10"?y="218"?width="393"?height="200"?backgroundColor="#84DACF">??
  2. ??<mx:Label?x="4"?y="10"?text="UserID:"/>??
  3. ??<mx:Label?x="60"?y="83"?width="160"?id="labName"?color="#C52167"?fontSize="17"/>??
  4. ??<mx:Label?x="60"?y="109"?width="160"?id="labPosition"?color="#C52167"?fontSize="17"/>??
  5. ??<mx:Label?x="60"?y="57"?width="160"?id="labNikeName"?color="#C52167"?fontSize="17"/>??
  6. ??<mx:Button?x="228"?y="8"?label="searchWebService"?width="131"?click="requestWebService();"/>??
  7. ??<mx:TextInput?x="60"?y="8"?id="txtUserID"/>??
  8. ?</mx:Canvas>??

?
?? 引入WebService组件(<mx:operation></mx:operation>节点定义WebService定义的方法信息(对应WSDL文件的方法描述)可以为多个):
?

[c-sharp] view plaincopyprint?
  1. <mx:WebService?id="uReqWebService"??wsdl="http://localhost:2222/FlexService/SearchUserService.asmx?WSDL">??
  2. ??<mx:operation?name="SearchUserName"?resultFormat="object"?fault="serviceReqFault(event)"?result="serviceReqResult(event)">??
  3. ??</mx:operation>??
  4. ?</mx:WebService>??

?
??? Flex中发送请求:

[c-sharp] view plaincopyprint?
  1. private?function?requestWebService():void??
  2. {??
  3. ?uReqWebService.SearchUserName(txtUserID.text);?//<mx:operation/>定义的方法 ??
  4. ?labMsg.alpha?=?1;??
  5. }??

?

?

??? Flex中接收返回数据:

[c-sharp] view plaincopyprint?
  1. private?function?serviceReqResult(e:ResultEvent):void??
  2. {??
  3. ?if(e.result.toString().length?>?0)??
  4. ?{??
  5. ??var?arry:XML?=?XML(e.result);??
  6. ??if(arry?!=?null?&&?arry.length()>?0)??
  7. ??{??
  8. ???labName.text?=?arry.children()[0];??
  9. ???labNikeName.text?=?arry.children()[1];??
  10. ???labPosition.text?=?arry.children()[2];??
  11. ??}??
  12. ?}??
  13. ?else???
  14. ?{??
  15. ??labName.text?=?"";??
  16. ??labNikeName.text?=?"";??
  17. ??labPosition.text?=?"";??
  18. ??Alert.show("查不到匹配的用户信息!");??
  19. ?}??
  20. ?labMsg.alpha?=?0;??
  21. }??

?

?

??? Flex中错误处理:

[c-sharp] view plaincopyprint?
  1. private?function?serviceReqFault(e:FaultEvent):void??
  2. {??
  3. ?Alert.show("请求WebService出错!原因是:"?+?e.message);??
  4. ?labMsg.alpha?=?0;??
  5. }??


结果如图:

?


??? /img/2012/10/18/1204381275.jpg

  相关解决方案