flex 访问WebService的方法有很多种,使用FLEX4中的"数据/服务"功能可以自动生成访问WebService的代理类,这样可以避免把所有的数据访问都写到MXML页面上,便于重复利用,同时可以直接导入后台自定义数据类型,方便传参。

直接上代码:其中WebService接口

  1. namespace MyNetWebService
  2. {
  3. /// <summary>
  4. /// MyWebService 的摘要说明
  5. /// </summary>
  6. [WebService(Namespace = "http://tempuriTemp.org/")]
  7. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  8. [System.ComponentModel.ToolboxItem(false)]
  9. // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
  10. // [System.Web.Script.Services.ScriptService]
  11. public class MyWebService : System.Web.Services.WebService
  12. {
  13.  
  14. [WebMethod]
  15. public string HelloWorld()
  16. {
  17. return "Hello World";
  18. }
  19.  
  20. [WebMethod]
  21. public Model[] GetDetailResult(SearchParameter parmeter, Staff staff)
  22. {
  23. return ModelHelp.GetSaleDetailResult(parmeter, staff);
  24. }
  25. }
  26. }

添加WebService服务:

连接数据/服务—>Web服务—>WSDL URL:  填写服务地址(http://localhost/XXX/MyWebService.asmx?WSDL)

使用FLEX4中的"数据/服务"功能 在services 下生成的代理类:

        

数据/服务 下 导入了webService的方法 和 自定义类型

自动生成访问WebService的代理类_Super_MyWebService.as

  1. /**
  2. * This is a generated class and is not intended for modification. To customize behavior
  3. * of this service wrapper you may modify the generated sub-class of this class - MyWebService.as.
  4. */
  5. package services.mywebservice
  6. {
  7. import com.adobe.fiber.core.model_internal;
  8. import com.adobe.fiber.services.wrapper.WebServiceWrapper;
  9. import com.adobe.serializers.utility.TypeUtility;
  10. import mx.rpc.AbstractOperation;
  11. import mx.rpc.AsyncToken;
  12. import mx.rpc.soap.mxml.Operation;
  13. import mx.rpc.soap.mxml.WebService;
  14. import valueObjects.DetailSearchParameter;
  15. import valueObjects.Employee;
  16. import valueObjects.Sale;
  17.  
  18. [ExcludeClass]
  19. internal class _Super_MyWebService extends com.adobe.fiber.services.wrapper.WebServiceWrapper
  20. {
  21.  
  22. // Constructor
  23. public function _Super_MyWebService()
  24. {
  25. // initialize service control
  26. _serviceControl = new mx.rpc.soap.mxml.WebService();
  27. var operations:Object = new Object();
  28. var operation:mx.rpc.soap.mxml.Operation;
  29.  
  30. operation = new mx.rpc.soap.mxml.Operation(null, "HelloWorld");
  31. operation.resultType = String;
  32. operations["HelloWorld"] = operation;
  33.  
  34. operation = new mx.rpc.soap.mxml.Operation(null, "GetDetailResult");
  35. operation.resultElementType = valueObjects.Sale;
  36. operations["GetDetailResult"] = operation;
  37.  
  38. _serviceControl.operations = operations;
  39. try
  40. {
  41. _serviceControl.convertResultHandler = com.adobe.serializers.utility.TypeUtility.convertResultHandler;
  42. }
  43. catch (e: Error)
  44. { /* Flex 3.4 and eralier does not support the convertResultHandler functionality. */ }
  45.  
  46. _serviceControl.service = "MyWebService";
  47. _serviceControl.port = "MyWebServiceSoap";
  48. wsdl = "http://localhost/XXX/MyWebService.asmx?WSDL";
  49. model_internal::loadWSDLIfNecessary();
  50.  
  51. model_internal::initialize();
  52. }
  53.  
  54. /**
  55. * This method is a generated wrapper used to call the 'HelloWorld' operation. It returns an mx.rpc.AsyncToken whose
  56. * result property will be populated with the result of the operation when the server response is received.
  57. * To use this result from MXML code, define a CallResponder component and assign its token property to this method's return value.
  58. * You can then bind to CallResponder.lastResult or listen for the CallResponder.result or fault events.
  59. *
  60. * @see mx.rpc.AsyncToken
  61. * @see mx.rpc.CallResponder
  62. *
  63. * @return an mx.rpc.AsyncToken whose result property will be populated with the result of the operation when the server response is received.
  64. */
  65. public function HelloWorld() : mx.rpc.AsyncToken
  66. {
  67. model_internal::loadWSDLIfNecessary();
  68. var _internal_operation:mx.rpc.AbstractOperation = _serviceControl.getOperation("HelloWorld");
  69. var _internal_token:mx.rpc.AsyncToken = _internal_operation.send() ;
  70.  
  71. return _internal_token;
  72. }
  73.  
  74. /**
  75. * This method is a generated wrapper used to call the 'GetDetailResult' operation. It returns an mx.rpc.AsyncToken whose
  76. * result property will be populated with the result of the operation when the server response is received.
  77. * To use this result from MXML code, define a CallResponder component and assign its token property to this method's return value.
  78. * You can then bind to CallResponder.lastResult or listen for the CallResponder.result or fault events.
  79. *
  80. * @see mx.rpc.AsyncToken
  81. * @see mx.rpc.CallResponder
  82. *
  83. * @return an mx.rpc.AsyncToken whose result property will be populated with the result of the operation when the server response is received.
  84. */
  85. public function GetDetailResult(parmeter:valueObjects.DetailSearchParameter, loginEmp:valueObjects.Employee) : mx.rpc.AsyncToken
  86. {
  87. model_internal::loadWSDLIfNecessary();
  88. var _internal_operation:mx.rpc.AbstractOperation = _serviceControl.getOperation("GetDetailResult");
  89. var _internal_token:mx.rpc.AsyncToken = _internal_operation.send(parmeter,loginEmp) ;
  90.  
  91. return _internal_token;
  92. }
  93.  
  94. }
  95.  
  96. }

 自动生成访问WebService的代理类MyWebService.as

  1. /**
  2. * This is a generated sub-class of _MyWebService.as and is intended for behavior
  3. * customization. This class is only generated when there is no file already present
  4. * at its target location. Thus custom behavior that you add here will survive regeneration
  5. * of the super-class.
  6. **/
  7.  
  8. package services.mywebservice
  9. {
  10.  
  11. public class MyWebService extends _Super_MyWebService
  12. {
  13.  
  14. }
  15.  
  16. }

Flex 端Temp.mxml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Module xmlns:fx="http://ns.adobe.com/mxml/2009"
  3. xmlns:s="library://ns.adobe.com/flex/spark"
  4. xmlns:mx="library://ns.adobe.com/flex/mx"
  5. layout="vertical" width="100%" height="100%"
  6. xmlns:common="common.*"
  7. xmlns:mywebservice="services.mywebservice.*"
  8. >
  9. <fx:Script>
  10. <![CDATA[
  11. import mx.events.FlexEvent;
  12. import mx.rpc.events.ResultEvent;
  13. import mx.rpc.soap.WebService;
  14. import mx.controls.Alert;
  15.  
  16. protected function btn_call_clickHandler(event:MouseEvent):void
  17. {
  18. // TODO Auto-generated method stub
  19. getresult.token=MyWebService.HelloWorld();
  20. }
  21.  
  22. protected function getresult_resultHandler(event:ResultEvent):void
  23. {
  24. // TODO Auto-generated method stub
  25. if(event.result!=null)
  26. {
  27. resultweb.text=event.result as String;
  28. }
  29. }
  30.  
  31. ]]>
  32. </fx:Script>
  33. <!-- 引用css样式 -->
  34. <fx:Style source="css/style.css" />
  35.  
  36. <fx:Declarations>
  37. <!-- 将非可视元素(例如服务、值对象)放在此处 -->
  38. <mywebservice:MyWebService id="MyWebService" showBusyCursor="true" fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)"/>
  39. <s:CallResponder id="getresult" result="getresult_resultHandler(event)" />
  40. </fx:Declarations>
  41. <s:VGroup width="100%" height="100%" paddingLeft="" paddingRight="" paddingBottom="" paddingTop="">
  42. <s:HGroup width="100%" verticalAlign="middle">
  43.  
  44. <mx:Text id="resultweb"/>
  45. <common:Cbutton id="btn_call" label="调用webService" click="btn_call_clickHandler(event)" />
  46. </s:HGroup>
  47. <s:HGroup width="100%" verticalAlign="middle">
  48. <s:Label verticalAlign="middle" styleName="msgTxtStyle" width="100%" id="msg_label"/>
  49. </s:HGroup>
  50. </s:VGroup>
  51. </mx:Module >

运行结果:

flex 调用WebService2(基于.net)的更多相关文章

  1. flex 调用WebService1(基于.net)

    以.net平台下C#语言开发的WebService为web服务,使用flex  actionscript语句访问webservice接口 Flex:  Temp.mxml部分代码 //调用WebSer ...

  2. Flex调用java webservice

    <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...

  3. Flex 调用webService

    今天手头没事,就学习下 Flex 调用webService的方法.本地测试OK  和大家分享下. ——————————————————————————————————————————————————— ...

  4. Flex与Java交互(Flex调用java类展示数据)解析xml展示数据

    Flex与java通信最简单例子(详细说明了各种需要注意的配置):http://blog.csdn.net/u010011052/article/details/9116869 Flex与java通信 ...

  5. flex调用JS报安全沙箱错误解决办法

    flex调用JS方法弹窗时一般会报安全沙箱错误,只要将被调用的JS方法设置延时就可解决. function openKqQuery(){ window.showModalDialog("pa ...

  6. Flex调用JavaScript获取文件路径

    Flex的Web中有FileReference的类可以对文件操作,实现上传.下载的功能,但是没有办法获取到文件的路径. 普遍的方法是Flex调用JavaScript的文件浏览功能来获取文件路径. 1. ...

  7. flex 调用gp服务

    同步异步说明: gp服务分为同步和异步两种模式,两者的区别是:同步:适合于快速的处理,数据量较小,本质区别在于同步模式,服务器处理之后,处理结果并不在服务器端保存,而是将结果发送至客户端,由客户端去显 ...

  8. flex调用webservice中的datatable结果写入datagrid

    webservice配置文件 <appSettings> <add key="sqlConDuke" value="server=10.9.34.88; ...

  9. JAVA WEBSERVICE服务端&客户端的配置及调用(基于JDK)

    前言:我之前是从事C#开发的,因公司项目目前转战JAVA&ANDROID开发,由于对JAVA的各种不了解,遇到的也是重重困难.目前在做WEBSERVICE提供数据支持,看了网上相关大片的资料也 ...

随机推荐

  1. jquery的uploadify上传jsp+servlet

    1.准备材料:下载jquery.uploadify上传js   注意:这个上传在firefox下会出现问题如果你在项目中加了拦截器,因为session会丢失,所以你可以传参的时候带上你所需要的条件,在 ...

  2. slf4j教程

    slf4j只是一个门面(facet),它不包含具体的实现,而是将一些log4j,java.logging等实现包装成统一的接口.借用下图展示了常用日志文件的关系: 通过上面的图,可以简单的理清关系! ...

  3. 16-js-缓冲运动

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. html5 百分比计算

    这几天一直在看html5,看到了百分比的计算公式:目标元素的尺寸/上下文元素的尺寸=百分比尺寸.看到这个公式,有点懂,但是有不明白.对于目标元素很容易理解,但是对于上下文元素就不是很好理解了.试了一些 ...

  5. css 溢出文本显示省略号

    这个标题其实已经是一个老生常谈的问题了.很多时候,比如网站最基本的文章列表,标题会很长,而显示列表的区域宽度却没有这么宽,这时候最正常的做法就是 让超出宽度的部分文字用省略号(…)来表示.通常做法是网 ...

  6. 递归:这帮坑爹的小兔崽子 - 零基础入门学习Python023

    递归:这帮坑爹的小兔崽子 让编程改变世界 Change the world by program 斐波那契数列的递归实现 这节课我们用斐波那契(Fibonacci)数列的递归实现来作为第一个例子吧,斐 ...

  7. AngularJS中的控制器示例_3

    <!doctype html> <html ng-app="myApp"> <head> <script src="C:\\Us ...

  8. 2014年总结:我的IT路

    又是一年春节时,转眼之间已经毕业4年,简单回顾一下这几年的职业生涯,希望大家提出宝贵意见. 大学时,几个同学跟着学校网络中心的老师一块做校园网上运行的小系统.偶尔协助一下老师对学校机房.校园网做一下维 ...

  9. dubbo初体验

    最近需要开发部门中某个大数据量的提取的功能,加到了一个ElasticSearch的群.在群里听说到一个框架叫dubbo,阿里系开源软件.听到群友谈的神乎其神的,什么什么功能切分多协议栈,高并发等等等. ...

  10. 【转】Linux中history历史命令使用方法详解

    原文网址:http://os.51cto.com/art/201205/335040.htm 当你在玩Linux的时候,如果你经常使用命令行来控制你的Linux系统,那么有效地使用命令历史机制将会使效 ...