flex 訪問WebService的方法有很多種,使用FLEX4中的"數據/服務"功能可以自動生成訪問WebService的代理類,這樣可以避免把所有的數據訪問都寫到Mxml頁面上,便于重復利用,同時可以直接導入后臺自定義數據類型,方便傳參。
直接上代碼:其中WebService接口
namespace MyNetWebService{ /// <summary> /// MyWebService 的摘要說明 /// </summary> [WebService(Namespace = "http://tempuriTemp.org/")] [WebServiceBinding(ConformsTo = Wsiprofiles.BasicPRofile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允許使用 asp.net Ajax 從腳本中調用此 Web 服務,請取消對下行的注釋。 // [System.Web.Script.Services.ScriptService] public class MyWebService : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } [WebMethod] public Model[] GetDetailResult(SearchParameter parmeter, Staff staff) { return ModelHelp.GetSaleDetailResult(parmeter, staff); } } }
添加WebService服務:
連接數據/服務—>Web服務—>WSDL URL: 填寫服務地址(http://localhost/XXX/MyWebService.asmx?WSDL)
使用FLEX4中的"數據/服務"功能 在services 下生成的代理類:
數據/服務 下 導入了webService的方法 和 自定義類型
自動生成訪問WebService的代理類_Super_MyWebService.as
/** * This is a generated class and is not intended for modification. To customize behavior * of this service wrapper you may modify the generated sub-class of this class - MyWebService.as. */package services.mywebservice{import com.adobe.fiber.core.model_internal;import com.adobe.fiber.services.wrapper.WebServiceWrapper;import com.adobe.serializers.utility.TypeUtility;import mx.rpc.AbstractOperation;import mx.rpc.AsyncToken;import mx.rpc.soap.mxml.Operation;import mx.rpc.soap.mxml.WebService;import valueObjects.DetailSearchParameter;import valueObjects.Employee;import valueObjects.Sale;[ExcludeClass]internal class _Super_MyWebService extends com.adobe.fiber.services.wrapper.WebServiceWrapper{ // Constructor public function _Super_MyWebService() { // initialize service control _serviceControl = new mx.rpc.soap.mxml.WebService(); var operations:Object = new Object(); var operation:mx.rpc.soap.mxml.Operation; operation = new mx.rpc.soap.mxml.Operation(null, "HelloWorld"); operation.resultType = String; operations["HelloWorld"] = operation; operation = new mx.rpc.soap.mxml.Operation(null, "GetDetailResult"); operation.resultElementType = valueObjects.Sale; operations["GetDetailResult"] = operation; _serviceControl.operations = operations; try { _serviceControl.convertResultHandler = com.adobe.serializers.utility.TypeUtility.convertResultHandler; } catch (e: Error) { /* Flex 3.4 and eralier does not support the convertResultHandler functionality. */ } _serviceControl.service = "MyWebService"; _serviceControl.port = "MyWebServiceSoap"; wsdl = "http://localhost/XXX/MyWebService.asmx?WSDL"; model_internal::loadWSDLIfNecessary(); model_internal::initialize(); } /** * This method is a generated wrapper used to call the 'HelloWorld' operation. It returns an mx.rpc.AsyncToken whose * result property will be populated with the result of the operation when the server response is received. * To use this result from MXML code, define a CallResponder component and assign its token property to this method's return value. * You can then bind to CallResponder.lastResult or listen for the CallResponder.result or fault events. * * @see mx.rpc.AsyncToken * @see mx.rpc.CallResponder * * @return an mx.rpc.AsyncToken whose result property will be populated with the result of the operation when the server response is received. */ public function HelloWorld() : mx.rpc.AsyncToken { model_internal::loadWSDLIfNecessary(); var _internal_operation:mx.rpc.AbstractOperation = _serviceControl.getOperation("HelloWorld"); var _internal_token:mx.rpc.AsyncToken = _internal_operation.send() ; return _internal_token; } /** * This method is a generated wrapper used to call the 'GetDetailResult' operation. It returns an mx.rpc.AsyncToken whose * result property will be populated with the result of the operation when the server response is received. * To use this result from MXML code, define a CallResponder component and assign its token property to this method's return value. * You can then bind to CallResponder.lastResult or listen for the CallResponder.result or fault events. * * @see mx.rpc.AsyncToken * @see mx.rpc.CallResponder * * @return an mx.rpc.AsyncToken whose result property will be populated with the result of the operation when the server response is received. */ public function GetDetailResult(parmeter:valueObjects.DetailSearchParameter, loginEmp:valueObjects.Employee) : mx.rpc.AsyncToken { model_internal::loadWSDLIfNecessary(); var _internal_operation:mx.rpc.AbstractOperation = _serviceControl.getOperation("GetDetailResult"); var _internal_token:mx.rpc.AsyncToken = _internal_operation.send(parmeter,loginEmp) ; return _internal_token; } }}
自動生成訪問WebService的代理類MyWebService.as
/** * This is a generated sub-class of _MyWebService.as and is intended for behavior * customization. This class is only generated when there is no file already present * at its target location. Thus custom behavior that you add here will survive regeneration * of the super-class. **/ package services.mywebservice{public class MyWebService extends _Super_MyWebService{ }}
Flex 端Temp.mxml
<?xml version="1.0" encoding="utf-8"?><mx:Module xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" layout="vertical" width="100%" height="100%" xmlns:common="common.*" xmlns:mywebservice="services.mywebservice.*" > <fx:Script> <![CDATA[ import mx.events.FlexEvent; import mx.rpc.events.ResultEvent; import mx.rpc.soap.WebService; import mx.controls.Alert; protected function btn_call_clickHandler(event:MouseEvent):void { // TODO Auto-generated method stub getresult.token=MyWebService.HelloWorld(); } protected function getresult_resultHandler(event:ResultEvent):void { // TODO Auto-generated method stub if(event.result!=null) { resultweb.text=event.result as String; } } ]]> </fx:Script> <!-- 引用CSS樣式 --> <fx:Style source="css/style.css" /> <fx:Declarations> <!-- 將非可視元素(例如服務、值對象)放在此處 --> <mywebservice:MyWebService id="MyWebService" showBusyCursor="true" fault="Alert.show(event.fault.faultString + '/n' + event.fault.faultDetail)"/> <s:CallResponder id="getresult" result="getresult_resultHandler(event)" /> </fx:Declarations> <s:VGroup width="100%" height="100%" paddingLeft="10" paddingRight="10" paddingBottom="10" paddingTop="3"> <s:HGroup width="100%" verticalAlign="middle"> <mx:Text id="resultweb"/> <common:Cbutton id="btn_call" label="調用webService" click="btn_call_clickHandler(event)" /> </s:HGroup> <s:HGroup width="100%" verticalAlign="middle"> <s:Label verticalAlign="middle" styleName="msgTxtStyle" width="100%" id="msg_label"/> </s:HGroup> </s:VGroup></mx:Module >
運行結果:
新聞熱點
疑難解答