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