【问题标题】:Unable to retrieve value from java web service response in Flex无法从 Flex 中的 Java Web 服务响应中检索值
【发布时间】:2011-09-21 13:57:35
【问题描述】:

我正在尝试从 flex mxml/action 脚本中读取来自 java web 服务的数据,似乎调用,即请求/响应成功,但是当无法从响应中读取值时,请帮助 MXML 代码如下:

Unable to print the value from :
var outA:OutputA_type = ccxc.OutputA;
 trace(outA);
var aaa:String = outA.toString();
Alert.show(aaa)

它打印为[Object OutputA)_Type],但不是所需的值。但是在 flex builder 测试连接中,也在网络监控中我看到了正确的值/响应。

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx"
               xmlns:number1="services.number1.*"
               xmlns:testws="services.testws.*"
               minWidth="955" minHeight="850" creationComplete="initApp()">


    <!-- Styles ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->

    <fx:Style source="Styles.css"/>

    <!-- Script ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->

    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.collections.ArrayList;
            import mx.controls.Alert;
            import mx.events.CalendarLayoutChangeEvent;
            import mx.rpc.AsyncToken;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;
            import mx.utils.ObjectUtil;

            import services.testws.Testws;

            import valueObjects.OutputA_type;
            import valueObjects.Parameters_type;
            import valueObjects.TestParameters;
            import valueObjects.TestResult_type;

            private function initApp():void  {

                var testWebS:Testws = new Testws();
                var testParam:TestParameters = new TestParameters();
                testParam.ParamA = 2;
                testParam.ParamB = 3;

                var token:AsyncToken = testWebS.test(testParam);

                token.addResponder(new mx.rpc.Responder(result, fault));
            }

            private function result(event:ResultEvent) : void {
                trace(event.result);
                var strData:TestResult_type = event.result as  TestResult_type;
                var ccxc:Parameters_type = strData.Parameters;
                var outA:OutputA_type = ccxc.OutputA;
                trace(outA);
                var aaa:String = outA.toString();

                Alert.show(aaa.toString());

            }

            public function fault(event : FaultEvent) : void {
                Alert.show("Failed Condition Fault Exception");
            }

        ]]>
    </fx:Script>

    <!-- Declarations ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->

    <fx:Declarations>

    </fx:Declarations>

    <!-- UI components ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->

    <s:Label x="10" y="34" 
             width="690" height="40" 
             text="Employee Portal: Vehicle Request Form"
             styleName="titleHeader"/>

    <s:Form x="10" y="70">
        <s:FormItem label="Input a:">
            <s:TextInput id="a"/>
        </s:FormItem>
        <s:FormItem label="Input b:">
            <s:TextInput id="b"/>
        </s:FormItem>
        <s:FormItem>
            <s:Button id="submitButton" 
                      label="Submit Request" click="submitButton_clickHandler(event)"/>
        </s:FormItem>
    </s:Form>

</s:Application>


SOAP-UI Request:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tes="http://tempuri.org/testws">
   <soapenv:Header/>
   <soapenv:Body>
      <tes:test>
         <tes:parameters>
            <!--Optional:-->
            <tes:ParamA>3</tes:ParamA>
            <!--Optional:-->
            <tes:ParamB>1</tes:ParamB>
         </tes:parameters>
      </tes:test>
   </soapenv:Body>
</soapenv:Envelope>

SOAP-UI Response:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <m:testResponse xmlns:m="http://tempuri.org/testws">
         <m:testResult>
            <axis2ns794:Parameters xmlns:axis2ns794="http://tempuri.org/testws">
               <axis2ns795:OutputA description="" xmlns:axis2ns795="http://tempuri.org/testws">1</axis2ns795:OutputA>
            </axis2ns794:Parameters>
         </m:testResult>
      </m:testResponse>
   </soapenv:Body>
</soapenv:Envelope>

【问题讨论】:

    标签: java apache-flex web-services actionscript


    【解决方案1】:

    而不是使用

     private function result(event:ResultEvent) : void {
                trace(event.result);
                var strData:TestResult_type = event.result as  TestResult_type;
                var ccxc:Parameters_type = strData.Parameters;
                var outA:OutputA_type = ccxc.OutputA;
                trace(outA);
                var aaa:String = outA.toString();
    
                Alert.show(aaa.toString());
    
            }
    

    尝试使用 Object 而不是直接转换它。

    var retObj:Object = event.result;
    var strData:TestResult_type = new TestResult_type();
    strData.firstProperty = retObj.firstProperty;
    strData.secondProperty = retObj.secondProperty;
    

    我认为我之前遇到了一个问题,您不能像在 java 端那样将返回的对象分配给 flex 对象。如果这可行,那么您必须遍历每个 event.result 对象并通过 setter 设置对象的所有属性。

    【讨论】:

    • +1 Flex 本身无法将返回的数据转换为类型化对象(除非您使用带有映射的远程对象)。因此,接受结果作为通用对象,并通过循环其属性或通过一些工厂方法将其转换为类型化对象。
    • 如果我将 strData 设置为 strData:Object = event.result,如何读取数据?
    • 抱歉延迟评论,如果您希望它是 strData,您必须遍历返回的对象并通过将它们的属性设置为返回对象的属性来创建 TestResult_type 对象。如果它只是一个对象,您只需创建一个 TestResult_type 变量并将其属性设置为结果中的每个属性。这样您就可以将其用作 TestResult_type
    • 迈克,我厌倦了遵循上述建议,但是一旦我有了 OutA 如何从中检索价值。下面是我使用的代码 sn-p:var retObj:Object = event.result; var aa:TestResult_type = new TestResult_type(); var bb:Parameters_type =新的Parameters_type(); var outAA:OutputA_type = new OutputA_type(); aa.Parameters = retObj.Parameters; bb = retObj.Parameters; outAA = bb.OutputA;
    • 我不太明白你的问题。你仍然收到错误吗?如果您有 OutA,它应该是 OutputA_type,并且您应该能够像访问 OutputA_type 的任何对象一样访问它的属性。您能否更具体地说明您在寻找什么?
    【解决方案2】:
    private function result(event:ResultEvent) : void { 
                    trace(event.result); 
                    var strData:TestResult_type = event.result as  TestResult_type; 
                    var ccxc:Parameters_type = strData.Parameters; 
                    var outA:OutputA_type = ccxc.OutputA; 
                    trace(outA); 
                    var aaa:String = outA.toString(); 
    
                    Alert.show(aaa.toString()); 
    
    } 
    
    1. 您在执行trace(event.result) 时得到的响应是什么?
    2. 跟踪这个strData,把你得到的记录下来。

    【讨论】:

    • trace(event.result) 返回:[object TestResult_type] 和 trace(outA) 返回 [object OutputA_type]
    猜你喜欢
    • 2016-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多