Flex连接WebService实例

前端之家收集整理的这篇文章主要介绍了Flex连接WebService实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
获取网络发布的webservice
 
GetMonths.mxml
 
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white" viewSourceURL="srcview/index.html">
    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.rpc.events.ResultEvent;
            import mx.rpc.events.FaultEvent;
            import mx.utils.ObjectUtil;
            private var startTime:int;
            private var endTime:int;
            private function button_click():void {
                webService.getMonths.send();
                startTime = getTimer();
                lbl.text = "";
            }
            private function getMonths_result(evt:ResultEvent):void {
                textArea.text = ObjectUtil.toString(evt.result);
                calcTime();
            }
            private function getMonths_fault(evt:FaultEvent):void {
                Alert.show(evt.type);
                calcTime();
            }
            private function calcTime():void {
                endTime = getTimer();
                lbl.text = "total time: " + (endTime - startTime) + "ms";
            }
        ]]>
    </mx:Script>
    <mx:WebService id="webService"
            wsdl=" http://www.flash-mx.com/ws/months.cfc?wsdl">
        <mx:operation name="getMonths"
                resultFormat="object"
                result="getMonths_result(event);"
                fault="getMonths_fault(event);" />
    </mx:WebService>
    <mx:ApplicationControlBar dock="true">
        <mx:Button id="button"
                label="get months from web service"
                click="button_click();" />
        <mx:Spacer width="100%" />
        <mx:Label id="lbl" />
    </mx:ApplicationControlBar>
    <mx:TextArea id="textArea"
            editable="false"
            width="100%"
            height="100%" />
</mx:Application>
=================================================================
使用自己发布的WebService与Flex通信
 
<?@H_502_94@xml version="1.0" encoding="UTF-8" ?>
- < @H_404_107@wsdl:definitions@H_404_107@ targetNamespace =" http://service.jy.com " xmlns:soapenc12 =" http://www.w3.org/2003/05/soap-encoding " xmlns:tns =" http://service.jy.com " xmlns:wsdl =" http://schemas.xmlsoap.org/wsdl/ " xmlns:xsd =" http://www.w3.org/2001/XMLSchema " xmlns:soap11 =" http://schemas.xmlsoap.org/soap/envelope/ " xmlns:wsdlsoap =" http://schemas.xmlsoap.org/wsdl/soap/ " xmlns:soapenc11 =" http://schemas.xmlsoap.org/soap/encoding/ " xmlns:soap12 =" http://www.w3.org/2003/05/soap-envelope ">
+ < @H_404_107@wsdl:types >
- < @H_404_107@xsd:schema xmlns:xsd =" http://www.w3.org/2001/XMLSchema " @H_404_107@ attributeFormDefault =" qualified " @H_404_107@ elementFormDefault =" qualified " @H_404_107@ targetNamespace =" http://service.jy.com ">
- < @H_404_107@xsd:element@H_404_107@ name =" getKey ">
- < @H_404_107@xsd:complexType >
- < @H_404_107@xsd:sequence >
  < @H_404_107@xsd:element @H_404_107@maxOccurs =" 1 " @H_404_107@ minOccurs =" 1 " @H_404_107@ name =" in0 " @H_404_107@ nillable =" true " @H_404_107@ type =" xsd:string " />
  < @H_404_107@xsd:element @H_404_107@maxOccurs =" 1 " @H_404_107@ minOccurs =" 1 " @H_404_107@ name =" in1 " @H_404_107@ nillable =" true " @H_404_107@ type =" xsd:string " />
  </ @H_404_107@xsd:sequence >
  </ @H_404_107@xsd:complexType >
  </ @H_404_107@xsd:element >
- < @H_404_107@xsd:element@H_404_107@ name =" getKeyResponse ">
- < @H_404_107@xsd:complexType >
- < @H_404_107@xsd:sequence >
  < @H_404_107@xsd:element @H_404_107@maxOccurs =" 1 " @H_404_107@ minOccurs =" 1 " @H_404_107@ name =" out " @H_404_107@ nillable =" true " @H_404_107@ type =" xsd:string " />
  </ @H_404_107@xsd:sequence >
  </ @H_404_107@xsd:complexType >
  </ @H_404_107@xsd:element >
- < @H_404_107@xsd:element@H_404_107@ name =" login ">
- < @H_404_107@xsd:complexType >
- < @H_404_107@xsd:sequence >
  < @H_404_107@xsd:element @H_404_107@maxOccurs =" 1 " @H_404_107@ minOccurs =" 1 " @H_404_107@ name =" in0 " @H_404_107@ nillable =" true " @H_404_107@ type =" xsd:string " />
  < @H_404_107@xsd:element @H_404_107@maxOccurs =" 1 " @H_404_107@ minOccurs =" 1 " @H_404_107@ name =" in1 " @H_404_107@ nillable =" true " @H_404_107@ type =" xsd:string " />
  </ @H_404_107@xsd:sequence >
  </ @H_404_107@xsd:complexType >
  </ @H_404_107@xsd:element >
- < @H_404_107@xsd:element@H_404_107@ name =" loginResponse ">
- < @H_404_107@xsd:complexType >
- < @H_404_107@xsd:sequence >
  < @H_404_107@xsd:element @H_404_107@maxOccurs =" 1 " @H_404_107@ minOccurs =" 1 " @H_404_107@ name =" out " @H_404_107@ type =" xsd:boolean " />
  </ @H_404_107@xsd:sequence >
  </ @H_404_107@xsd:complexType >
  </ @H_404_107@xsd:element >
  </ @H_404_107@xsd:schema >
  </ @H_404_107@wsdl:types >
- < @H_404_107@wsdl:message@H_404_107@ name =" getKeyResponse ">
  < @H_404_107@wsdl:part @H_404_107@name =" parameters " @H_404_107@ element =" tns:getKeyResponse " />
  </ @H_404_107@wsdl:message >
- < @H_404_107@wsdl:message@H_404_107@ name =" getKeyRequest ">
  < @H_404_107@wsdl:part @H_404_107@name =" parameters " @H_404_107@ element =" tns:getKey " />
  </ @H_404_107@wsdl:message >
- < @H_404_107@wsdl:message@H_404_107@ name =" loginResponse ">
  < @H_404_107@wsdl:part @H_404_107@name =" parameters " @H_404_107@ element =" tns:loginResponse " />
  </ @H_404_107@wsdl:message >
- < @H_404_107@wsdl:message@H_404_107@ name =" loginRequest ">
  < @H_404_107@wsdl:part @H_404_107@name =" parameters " @H_404_107@ element =" tns:login " />
  </ @H_404_107@wsdl:message >
- < @H_404_107@wsdl:portType@H_404_107@ name =" UserServicePortType ">
- < @H_404_107@wsdl:operation@H_404_107@ name =" getKey ">
  < @H_404_107@wsdl:input @H_404_107@name =" getKeyRequest " @H_404_107@ message =" tns:getKeyRequest " />
  < @H_404_107@wsdl:output @H_404_107@name =" getKeyResponse " @H_404_107@ message =" tns:getKeyResponse " />
  </ @H_404_107@wsdl:operation >
- < @H_404_107@wsdl:operation@H_404_107@ name =" login ">
  < @H_404_107@wsdl:input @H_404_107@name =" loginRequest " @H_404_107@ message =" tns:loginRequest " />
  < @H_404_107@wsdl:output @H_404_107@name =" loginResponse " @H_404_107@ message =" tns:loginResponse " />
  </ @H_404_107@wsdl:operation >
  </ @H_404_107@wsdl:portType >
- < @H_404_107@wsdl:binding@H_404_107@ name =" UserServiceHttpBinding " @H_404_107@ type =" tns:UserServicePortType ">
  < @H_404_107@wsdlsoap:binding @H_404_107@style =" document " @H_404_107@ transport =" http://schemas.xmlsoap.org/soap/http " />
- < @H_404_107@wsdl:operation@H_404_107@ name =" getKey ">
  < @H_404_107@wsdlsoap:operation @H_404_107@soapAction ="" />
- < @H_404_107@wsdl:input@H_404_107@ name =" getKeyRequest ">
  < @H_404_107@wsdlsoap:body @H_404_107@use =" literal " />
  </ @H_404_107@wsdl:input >
- < @H_404_107@wsdl:output@H_404_107@ name =" getKeyResponse ">
  < @H_404_107@wsdlsoap:body @H_404_107@use =" literal " />
  </ @H_404_107@wsdl:output >
  </ @H_404_107@wsdl:operation >
- < @H_404_107@wsdl:operation@H_404_107@ name =" login ">
  < @H_404_107@wsdlsoap:operation @H_404_107@soapAction ="" />
- < @H_404_107@wsdl:input@H_404_107@ name =" loginRequest ">
  < @H_404_107@wsdlsoap:body @H_404_107@use =" literal " />
  </ @H_404_107@wsdl:input >
- < @H_404_107@wsdl:output@H_404_107@ name =" loginResponse ">
  < @H_404_107@wsdlsoap:body @H_404_107@use =" literal " />
  </ @H_404_107@wsdl:output >
  </ @H_404_107@wsdl:operation >
  </ @H_404_107@wsdl:binding >
- < @H_404_107@wsdl:service@H_404_107@ name =" UserService ">
- < @H_404_107@wsdl:port@H_404_107@ name =" UserServiceHttpPort " @H_404_107@ binding =" tns:UserServiceHttpBinding ">
  < @H_404_107@wsdlsoap:address @H_404_107@location =" http://localhost:8081/xfireservice/service/UserService " />
  </ @H_404_107@wsdl:port >
  </ @H_404_107@wsdl:service >
  </ @H_404_107@wsdl:definitions >
 
===============================================================================
 
GetKeyFromXFire.mxml
<?xml version="1.0" encoding="utf-8"?>
<!-- 从WSDL为http://localhost:8081/xfireservice/service/UserService?wsdl的webservice执行getKey()操作 -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white" viewSourceURL="srcview/index.html">
    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.rpc.events.ResultEvent;
            import mx.rpc.events.FaultEvent;
            import mx.utils.ObjectUtil;
            private var startTime:int;
            private var endTime:int;
            private function button_click():void {
    //执行webservice的getKey(String in0,String in1)操作
                webService.getKey('fisher','123456');
                startTime = getTimer();
                lbl.text = "";
            }
            private function getKeys_result(evt:ResultEvent):void {
    //将getKey(String in0,String in1)结果赋予textArea
                textArea.text = ObjectUtil.toString(evt.result);
                calcTime();
            }
            private function getKeys_fault(evt:FaultEvent):void {
                Alert.show(evt.type);
                calcTime();
            }
            private function calcTime():void {
                endTime = getTimer();
                lbl.text = "total time: " + (endTime - startTime) + "ms";
            }
        ]]>
    </mx:Script>
 <!-- 获取webservice -->
    <mx:WebService id="webService"
            wsdl="http://localhost:8081/xfireservice/service/UserService?wsdl">
  <!-- 获取webservice的操作函数 -->
        <mx:operation name="getKey"
                resultFormat="object"
                result="getKeys_result(event);"
                fault="getKeys_fault(event);" />
    </mx:WebService>
    <mx:ApplicationControlBar dock="true">
        <mx:Button id="button"
                label="get key from web service"
                click="button_click();" />
        <mx:Spacer width="100%" />
        <mx:Label id="lbl" />
    </mx:ApplicationControlBar>
    <mx:TextArea id="textArea"
            editable="false"
            width="100%"
            height="100%" />
</mx:Application>
====================================================================
FlexWebService.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">
 <mx:WebService id="ws" wsdl=" http://localhost:8080/ws/services/wjhService?wsdl" useProxy="false" showBusyCursor="true">   <mx:operation name="put" result="Alert.show('恭喜!添加成功','返回框',3)" fault="Alert.show('添加失败','失败',3)">       </mx:operation>  <mx:operation name="get" result="showInfo(event)" fault="Alert.show('失败','tile',2)">       </mx:operation>   </mx:WebService>  <mx:XML>     </mx:XML>  <mx:Script>   <![CDATA[    import mx.rpc.events.ResultEvent;    import mx.controls.Alert;    private var com:String ;    private var cio:String ;        public function addCompany():void {     com = company.text ;     cio = ceo.text ;     ws.put(com,cio);    }        public function seeCeo():void {     var str:String;     str = company.text;     ws.get(str);    }        public function showInfo(event:ResultEvent):void {     var str:String;     str = event.result.toString();          Alert.show(str,"凯旋",2);    }   ]]>  </mx:Script>    <mx:Panel title="DataGrid Control Example" height="100%" width="100%"         paddingTop="10" paddingLeft="10" paddingRight="10">   <mx:Label x="10" y="10" text="公司" height="39" width="55" fontSize="21" fontWeight="bold"/>   <mx:TextInput x="61" y="10" height="31" id="company"/>   <mx:Label x="10" y="66" text="老大" width="55" height="49" fontSize="21" fontWeight="bold"/>   <mx:TextInput x="61" y="75" height="31" id="ceo"/>   <mx:Button label="添加" height="37" fontSize="17" id="add" click="addCompany();"/>   <mx:Button label="查看" height="29" fontSize="17" id="see" click="seeCeo();"/>     </mx:Panel> </mx:Application>

猜你在找的Flex相关文章