web-services - 如何使用 FLEX 解析 SOAP 响应

标签 web-services actionscript-3 apache-flex actionscript soap-client

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

根据我的研究,问题出在命名空间上,但仍然不确定如何解决。

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

我的java代码

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> 

我的弹性代码如下

<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:\dev\4.5.1\frameworks\projects\framework\src\mx\collections\ListCollectionView.as:870]
    at mx.binding::PropertyWatcher/updateProperty()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\binding\PropertyWatcher.as:338]
    at Function/http://adobe.com/AS3/2006/builtin::apply()
    at mx.binding::Watcher/wrapUpdate()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\binding\Watcher.as:192]
    at mx.binding::PropertyWatcher/updateParent()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\binding\PropertyWatcher.as:239]
    at mx.binding::Watcher/updateChildren()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\binding\Watcher.as:138]
    at mx.binding::PropertyWatcher/updateProperty()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\binding\PropertyWatcher.as:347]
    at Function/http://adobe.com/AS3/2006/builtin::apply()
    at mx.binding::Watcher/wrapUpdate()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\binding\Watcher.as:192]
    at mx.binding::PropertyWatcher/eventHandler()[E:\dev\4.5.1\frameworks\projects\framework\src\mx\binding\PropertyWatcher.as:375]
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at mx.rpc::CallResponder/set lastResult()
    at mx.rpc::CallResponder/result()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\rpc\CallResponder.as:120]
    at mx.rpc::AsyncToken/http://www.adobe.com/2006/flex/mx/internal::applyResult()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\rpc\AsyncToken.as:239]
    at mx.rpc.events::ResultEvent/http://www.adobe.com/2006/flex/mx/internal::callTokenResponders()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\rpc\events\ResultEvent.as:207]
    at mx.rpc::AbstractOperation/http://www.adobe.com/2006/flex/mx/internal::dispatchRpcEvent()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\rpc\AbstractOperation.as:244]
    at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::resultHandler()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\rpc\AbstractInvoker.as:318]
    at mx.rpc::Responder/result()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\rpc\Responder.as:56]
    at mx.rpc::AsyncRequest/acknowledge()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\rpc\AsyncRequest.as:84]
    at DirectHTTPMessageResponder/completeHandler()[E:\dev\4.5.1\frameworks\projects\rpc\src\mx\messaging\channels\DirectHTTPChannel.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

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

关于web-services - 如何使用 FLEX 解析 SOAP 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14309648/

相关文章:

web-services - ServiceStack 跨平台互操作性

actionscript-3 - 具有相同方法名的超接口(interface)和父类(super class)

actionscript-3 - AS3 Hamcrest - 断言一个数组包含另一个数组的全部

javascript - 节点上带有换行文本的 Flex TreeView

flash - 为 Mac 和 PC 编译 Flash

asp.net - 如何从另一个站点检索 Orchard CMS 内容?

c# - 创建JavascriptConverter时,如何返回一个数组?

web-services - 禁用自动生成的 JAX-WS 状态页面

java - 为什么 AS3 右移位与 Java 中的右移位不同?

apache-flex - 为什么 Flash Builder 4 使用 Flash Player 而不是浏览器来运行应用程序?