web服务——如何使用FLEX解析SOAP响应



我的Flex应用程序正确地调用了web服务,但它没有填充下拉框。

根据我的研究,这个问题是与命名空间,但仍然不知道如何解决它。

运行应用程序后,下拉框为空。

My java code

package com.Services;
import com.classes.*;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebResult;

@WebService (name="Hellos",    
targetNamespace="http://localhost:8081/Mywebservice2/services/Hellos")  
public class Hellos {
    @WebMethod
    public @WebResult (name="customers",partName="customers") Customer[] mycustomers()  
   {
        System.out.println("Retriving customers....");
        Customer[] cus = new Customer[2];
        cus[0] = new Customer("Jack", 28);
        cus[1] = new Customer("Ben", 29);
        return cus;
    }
}

flex应用程序的网络监视器特性显示响应如下

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/xml;charset=utf-8
Transfer-Encoding: chunked
Date: Thu, 10 Jan 2013 04:23:55 GMT
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope 
  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <soapenv:Body>
    <mycustomersResponse xmlns="http://Services.com">
      <mycustomersReturn>
          <age>28</age>
          <name>Jack</name>
      </mycustomersReturn>
      <mycustomersReturn>
          <age>29</age>
          <name>Ben</name>
      </mycustomersReturn>
   </mycustomersResponse>
 </soapenv:Body>
</soapenv:Envelope> 

我的flex代码如下

<fx:Script>
    <![CDATA[
        import mx.controls.Alert;
        import mx.events.FlexEvent;
        protected function  
                    dropDownList2_creationCompleteHandler(event:FlexEvent):void
        {
            mycustomersResult2.token = hellos.mycustomers();
        }
    ]]> 
</fx:Script>
    <fx:Declarations>
    <hellos:Hellos id="hellos" fault="Alert.show(event.fault.faultString + 'n' 
             + event.fault.faultDetail)"
             showBusyCursor="true"/>
    <s:CallResponder id="mycustomersResult2"/>
</fx:Declarations>
 <s:FormItem label="Label">
  <s:DropDownList id="dropDownList2"
       creationComplete="dropDownList2_creationCompleteHandler(event)"
       labelField="age">
       <s:AsyncListView list="{mycustomersResult2.lastResult}"/>
  </s:DropDownList>
  </s:FormItem>

当我改变

<s:AsyncListView list="{mycustomersResult2.lastResult}"/>

<s:AsyncListView list="   
               {mycustomersResult2.lastResult.mycustomersResponse.mycustomersReturn}"/>

给出以下错误

Error: Unknown Property: 'mycustomersResponse'.
    at mx.collections::ListCollectionView/http://www.adobe.com/2006/actionscript/flash/proxy::getProperty()[E:dev4.5.1frameworksprojectsframeworksrcmxcollectionsListCollectionView.as:870]
    at mx.binding::PropertyWatcher/updateProperty()[E:dev4.5.1frameworksprojectsframeworksrcmxbindingPropertyWatcher.as:338]
    at Function/http://adobe.com/AS3/2006/builtin::apply()
    at mx.binding::Watcher/wrapUpdate()[E:dev4.5.1frameworksprojectsframeworksrcmxbindingWatcher.as:192]
    at mx.binding::PropertyWatcher/updateParent()[E:dev4.5.1frameworksprojectsframeworksrcmxbindingPropertyWatcher.as:239]
    at mx.binding::Watcher/updateChildren()[E:dev4.5.1frameworksprojectsframeworksrcmxbindingWatcher.as:138]
    at mx.binding::PropertyWatcher/updateProperty()[E:dev4.5.1frameworksprojectsframeworksrcmxbindingPropertyWatcher.as:347]
    at Function/http://adobe.com/AS3/2006/builtin::apply()
    at mx.binding::Watcher/wrapUpdate()[E:dev4.5.1frameworksprojectsframeworksrcmxbindingWatcher.as:192]
    at mx.binding::PropertyWatcher/eventHandler()[E:dev4.5.1frameworksprojectsframeworksrcmxbindingPropertyWatcher.as:375]
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at mx.rpc::CallResponder/set lastResult()
    at mx.rpc::CallResponder/result()[E:dev4.5.1frameworksprojectsrpcsrcmxrpcCallResponder.as:120]
    at mx.rpc::AsyncToken/http://www.adobe.com/2006/flex/mx/internal::applyResult()[E:dev4.5.1frameworksprojectsrpcsrcmxrpcAsyncToken.as:239]
    at mx.rpc.events::ResultEvent/http://www.adobe.com/2006/flex/mx/internal::callTokenResponders()[E:dev4.5.1frameworksprojectsrpcsrcmxrpceventsResultEvent.as:207]
    at mx.rpc::AbstractOperation/http://www.adobe.com/2006/flex/mx/internal::dispatchRpcEvent()[E:dev4.5.1frameworksprojectsrpcsrcmxrpcAbstractOperation.as:244]
    at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::resultHandler()[E:dev4.5.1frameworksprojectsrpcsrcmxrpcAbstractInvoker.as:318]
    at mx.rpc::Responder/result()[E:dev4.5.1frameworksprojectsrpcsrcmxrpcResponder.as:56]
    at mx.rpc::AsyncRequest/acknowledge()[E:dev4.5.1frameworksprojectsrpcsrcmxrpcAsyncRequest.as:84]
    at DirectHTTPMessageResponder/completeHandler()[E:dev4.5.1frameworksprojectsrpcsrcmxmessagingchannelsDirectHTTPChannel.as:451]
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at flash.net::URLLoader/onComplete()

响应xml格式的名称空间似乎有问题:

<mycustomersResponse xmlns="http://Services.com">

只要在代码的任何地方写:

namespace ns = "http://Services.com";
use namespace ns;

我用给定的xml编写了演示:

var xml:XML = <soapenv:Envelope 
      xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
      xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     <soapenv:Body>
        <mycustomersResponse xmlns="http://Services.com">
          <mycustomersReturn>
              <age>28</age>
              <name>Jack</name>
          </mycustomersReturn>
          <mycustomersReturn>
              <age>29</age>
              <name>Ben</name>
          </mycustomersReturn>
       </mycustomersResponse>
     </soapenv:Body>
    </soapenv:Envelope> 
    namespace ns = "http://Services.com";
    use namespace ns;
    namespace ns_soapenv = "http://schemas.xmlsoap.org/soap/envelope/";
    use namespace ns_soapenv;
    trace(xml.Body.mycustomersResponse.mycustomersReturn.length()); //output 2

解析得很好。希望对大家有所帮助。

您不需要解析它,WebService类会为您完成它。

<s:WebService result=" soapResultHandler(event) ">
    <!-- do stuff here -->
</s:WebService>
<fx:Script>
    <![CDATA[
        private function soapResultHandler( e:ResultEvent ):void {
            //do stuff to e.result in here
        }
    ]]>
</fx:Script>

ResultEvent.result对象是自动解析为单个动态对象的XML。你可以很容易地遍历它来获得你需要的任何细节。值得注意的是,如果XML文档的单个级别具有相同标记名的多个实例,那么将会有一个ArrayCollection(注意,不是Array)而不是一系列这样的对象。

参见ResultEvent#result LiveDocs

相关内容

  • 没有找到相关文章

最新更新