2013-01-14 39 views
1

我的代碼應該解析SOAP響應並填充下拉列表。當我手動添加XML代碼它的工作原理,但是當我嘗試分析它運行到以下錯誤SOAP響應:我的flex代碼沒有正確解析肥皂響應

TypeError: Error #1009: Cannot access a property or method of a null object reference. 
at ex1_02_starter/dropDownList_creationCompleteHandler()[C:\Users\jack\Adobe Flash Builder 4.5\Workspace\Starter 1_02\src\ex1_02_starter.mxml:26] 
at ex1_02_starter/___ex1_02_starter_Operation1_result()[C:\Users\jack\Adobe Flash Builder 4.5\Workspace\Starter 1_02\src\ex1_02_starter.mxml:41] 
at flash.events::EventDispatcher/dispatchEventFunction() 
at flash.events::EventDispatcher/dispatchEvent() 
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:249] 
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() 

SOAP響應,我收到

<?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>Alex</name> 
     </mycustomersReturn> 
     <mycustomersReturn> 
     <age>29</age> 
     <name>Jack</name> 
     </mycustomersReturn> 
     <mycustomersReturn> 
     <age>30</age> 
     <name>Johny</name> 
     </mycustomersReturn> 
    </mycustomersResponse> 
    </soapenv:Body> 
</soapenv:Envelope> 

我的Flex代碼

<?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:components="components.*" 
       xmlns:hellos="services.hellos.*" 
       height="957" creationComplete="initApp()" > 
    <fx:Style source="Styles.css"/> 
    <fx:Script> 

     <![CDATA[ 
      import mx.collections.XMLListCollection; 
      import mx.events.FlexEvent; 
      import mx.messaging.messages.SOAPMessage; 
      import mx.rpc.events.ResultEvent; 
      [Bindable] 
      var _result:*; 
      private function initApp():void 
      { 
       mywebservice.mycustomers(); 
      } 
      protected function 
       dropDownList_creationCompleteHandler(event:ResultEvent):void 
      { 
       var xml:XML = event.result as XML; 
       var xmlString:String = xml.toXMLString(); 
       var patt:RegExp = new RegExp("xmlns[^\"]*\"[^\"]*\"", "gi"); 
       var newXmlString:String = xmlString.replace(patt, ""); 
       xml = new XML(newXmlString); 
       _result = new XMLListCollection(xml.mycustomersResponse.mycustomersReturn); 
      } 

     ]]> 
    </fx:Script> 
    <fx:Declarations> 
    <s:WebService id="mywebservice" 
        wsdl="http://localhost:8081/WebServiceTest/services/Hellos?wsdl"> 
     <s:operation name="mycustomers" 
         resultFormat="object" 
         result="dropDownList_creationCompleteHandler(event);" 
         /> 
     </s:WebService> 

    </fx:Declarations> 
    <s:FormItem label="Label"> 
     <s:DropDownList id="dropDownList" 

         labelField="name"> 
      <s:AsyncListView list="{_result}"/> 
     </s:DropDownList> 
    </s:FormItem> 
</s:Application> 
+0

大膽猜測:讓myService_ResultHandler公開? – doug65536

回答

1

來自這裏dropdown list does not show its values我稍微修改了我的例子。有兩次機會:從XML

  1. 地帶命名空間

  2. 利用命名空間

那些分別示於DROPDOWNLIST和dropDownList2代碼。

請注意,您不能利用名稱空間仍然使用labelField屬性(至少我找不到方法)。你必須使用labelFunction來提取標籤。

<?xml version="1.0" encoding="utf-8"?> 

<![CDATA[ 
     import mx.collections.XMLListCollection; 
     import mx.events.FlexEvent; 
     import mx.messaging.messages.SOAPMessage; 
     [Bindable] 
     var _result:*; 
     [Bindable] 
     var _result2:*; 

     protected function 
      dropDownList_creationCompleteHandler(event:FlexEvent):void 
     { 
      var xml:XML = <Body> 
          <myusersResponse xmlns="http://Services.com"> 
           <myusersReturn> 
           <name>Nicole</name> 
           <age>50</age> 
           </myusersReturn> 
           <myusersReturn> 
           <name>Jayne</name> 
           <age>40</age> 
           </myusersReturn> 
           <myusersReturn> 
           <name>Alex</name> 
           <age>33</age> 
           </myusersReturn> 
          </myusersResponse> 
          </Body>; 


      var xmlString:String = xml.toXMLString(); 
      var patt:RegExp = new RegExp("xmlns[^\"]*\"[^\"]*\"", "gi"); 
      var newXmlString:String = xmlString.replace(patt, ""); 
      xml = new XML(newXmlString); 
      _result = new XMLListCollection(xml.myusersResponse.myusersReturn); 

     } 

     protected function dropDownList2_creationCompleteHandler(event:FlexEvent):void 
     { 
      var xml:XML = <Body> 
          <myusersResponse xmlns="http://Services.com"> 
           <myusersReturn> 
           <name>Nicole</name> 
           <age>50</age> 
           </myusersReturn> 
           <myusersReturn> 
           <name>Jayne</name> 
           <age>40</age> 
           </myusersReturn> 
           <myusersReturn> 
           <name>Alex</name> 
           <age>33</age> 
           </myusersReturn> 
          </myusersResponse> 
          </Body>; 

      var ns:Namespace = new Namespace("http://Services.com"); 
      _result2 = new XMLListCollection(xml.ns::myusersResponse.ns::myusersReturn);     
     } 

     private function dropDownList2_labelFunction(item:Object):String{ 
      var itemXml:XML = item as XML; 
      var ns:Namespace = new Namespace("http://Services.com"); 
      return item.ns::name; 
     } 

    ]]> 
</fx:Script> 

<fx:Declarations> 

</fx:Declarations> 

<s:FormItem label="Label"> 
    <s:DropDownList id="dropDownList" 
        creationComplete="dropDownList_creationCompleteHandler(event)" 
        labelField="name"> 
     <s:AsyncListView list="{_result}"/> 
    </s:DropDownList> 
    <s:DropDownList id="dropDownList2" 
        creationComplete="dropDownList2_creationCompleteHandler(event)" 
        labelFunction="dropDownList2_labelFunction"> 
     <s:AsyncListView list="{_result2}"/> 
    </s:DropDownList>  
</s:FormItem> 

+0

感謝您的回答,它可以與預定義的XML協同工作,但是當我嘗試解析肥皂響應時無法正常工作,我已經更新了該問題,如果能夠幫助我,我將不勝感激。 –

+1

你可以檢查是否通過flex webservice類正確解析了soap envelope?請在這裏發佈trace(xmlString)的輸出。 –

+0

我不知道在哪裏可以找到這條痕跡的結果。調試一次到達var xmlString:String = xml.toXMLString();在控制檯中顯示此錯誤。 TypeError:錯誤#1009:無法訪問空對象引用的屬性或方法。 –

1

我認爲你應該使用event.result..mycustomersReturn。這將返回一個XMLList與兩個項目,然後您可以轉換並在您的下拉列表中使用。

+0

我已將其更改爲event.result.mycustomersReturn,但它遇到以下錯誤,錯誤:未知屬性:'mycustomersReturn'。 –

+0

然後你可能沒有得到你認爲你得到的XML。你有沒有嘗試用調試器檢查結果? – Eduardo

+0

我已更新問題 –

0

使用XML命名空間定義解析:

protected function dropDownList_creationCompleteHandler(event:ResultEvent):void 
      { 
       namespace ns = "http://Services.com"; 
       use namespace ns; 

       namespace ns_soapenv = "http://schemas.xmlsoap.org/soap/envelope/"; 
       use namespace ns_soapenv; 

       var xml:XML = event.result as XML;      
       _result = xml.Body.mycustomersResponse.mycustomersReturn; 
      }