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通信
 
<?xml version="1.0" encoding="UTF-8" ?>
- < wsdl:definitions 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 ">
+ < wsdl:types >
- < xsd:schema xmlns:xsd =" http://www.w3.org/2001/XMLSchema " attributeFormDefault =" qualified " elementFormDefault =" qualified " targetNamespace =" http://service.jy.com ">
- < xsd:element name =" getKey ">
- < xsd:complexType >
- < xsd:sequence >
  < xsd:element maxOccurs =" 1 " minOccurs =" 1 " name =" in0 " nillable =" true " type =" xsd:string " />
  < xsd:element maxOccurs =" 1 " minOccurs =" 1 " name =" in1 " nillable =" true " type =" xsd:string " />
  </ xsd:sequence >
  </ xsd:complexType >
  </ xsd:element >
- < xsd:element name =" getKeyResponse ">
- < xsd:complexType >
- < xsd:sequence >
  < xsd:element maxOccurs =" 1 " minOccurs =" 1 " name =" out " nillable =" true " type =" xsd:string " />
  </ xsd:sequence >
  </ xsd:complexType >
  </ xsd:element >
- < xsd:element name =" login ">
- < xsd:complexType >
- < xsd:sequence >
  < xsd:element maxOccurs =" 1 " minOccurs =" 1 " name =" in0 " nillable =" true " type =" xsd:string " />
  < xsd:element maxOccurs =" 1 " minOccurs =" 1 " name =" in1 " nillable =" true " type =" xsd:string " />
  </ xsd:sequence >
  </ xsd:complexType >
  </ xsd:element >
- < xsd:element name =" loginResponse ">
- < xsd:complexType >
- < xsd:sequence >
  < xsd:element maxOccurs =" 1 " minOccurs =" 1 " name =" out " type =" xsd:boolean " />
  </ xsd:sequence >
  </ xsd:complexType >
  </ xsd:element >
  </ xsd:schema >
  </ wsdl:types >
- < wsdl:message name =" getKeyResponse ">
  < wsdl:part name =" parameters " element =" tns:getKeyResponse " />
  </ wsdl:message >
- < wsdl:message name =" getKeyRequest ">
  < wsdl:part name =" parameters " element =" tns:getKey " />
  </ wsdl:message >
- < wsdl:message name =" loginResponse ">
  < wsdl:part name =" parameters " element =" tns:loginResponse " />
  </ wsdl:message >
- < wsdl:message name =" loginRequest ">
  < wsdl:part name =" parameters " element =" tns:login " />
  </ wsdl:message >
- < wsdl:portType name =" UserServicePortType ">
- < wsdl:operation name =" getKey ">
  < wsdl:input name =" getKeyRequest " message =" tns:getKeyRequest " />
  < wsdl:output name =" getKeyResponse " message =" tns:getKeyResponse " />
  </ wsdl:operation >
- < wsdl:operation name =" login ">
  < wsdl:input name =" loginRequest " message =" tns:loginRequest " />
  < wsdl:output name =" loginResponse " message =" tns:loginResponse " />
  </ wsdl:operation >
  </ wsdl:portType >
- < wsdl:binding name =" UserServiceHttpBinding " type =" tns:UserServicePortType ">
  < wsdlsoap:binding style =" document " transport =" http://schemas.xmlsoap.org/soap/http " />
- < wsdl:operation name =" getKey ">
  < wsdlsoap:operation soapAction ="" />
- < wsdl:input name =" getKeyRequest ">
  < wsdlsoap:body use =" literal " />
  </ wsdl:input >
- < wsdl:output name =" getKeyResponse ">
  < wsdlsoap:body use =" literal " />
  </ wsdl:output >
  </ wsdl:operation >
- < wsdl:operation name =" login ">
  < wsdlsoap:operation soapAction ="" />
- < wsdl:input name =" loginRequest ">
  < wsdlsoap:body use =" literal " />
  </ wsdl:input >
- < wsdl:output name =" loginResponse ">
  < wsdlsoap:body use =" literal " />
  </ wsdl:output >
  </ wsdl:operation >
  </ wsdl:binding >
- < wsdl:service name =" UserService ">
- < wsdl:port name =" UserServiceHttpPort " binding =" tns:UserServiceHttpBinding ">
  < wsdlsoap:address location =" http://localhost:8081/xfireservice/service/UserService " />
  </ wsdl:port >
  </ wsdl:service >
  </ 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相关文章