简体   繁体   English

Flex 4中AS的Web服务类

[英]Web Service Class of AS in Flex 4

I am trying to receive data from the Web Service and I am getting the Data from Web Service back but it is form of [object Object] . 我正在尝试从Web服务接收数据,并且正在从Web服务获取数据,但是它是[object Object]形式。 Can anybody help me on this. 有人可以帮我吗?

Below is the code for my web service: 以下是我的Web服务的代码:

public class WebServiceAccess
{

    private var webService:WebService;
    private var serviceOperation:AbstractOperation; 
    private var myValueObjects:ValueObjects;

    private var method:String;

    [Bindable]
    public var employeeData:ArrayCollection;
    [Bindable]
    public var employees:ArrayCollection;

    public function WebServiceAccess(url:String, method:String)
    {
        webService = new WebService();
        this.method = method;
        webService.loadWSDL(url);
        webService.addEventListener(LoadEvent.LOAD, ServiceRequest);
    }

    public function ServiceRequest():void
    {
        serviceOperation = webService.getOperation(method);
        serviceOperation.addEventListener(FaultEvent.FAULT, DisplayError);
        serviceOperation.addEventListener(ResultEvent.RESULT, DisplayResult); 
        serviceOperation.send(); 
    }

    public function DisplayError(evt:FaultEvent):void
    {
        Alert.show(evt.fault.toString());
    }

    public function DisplayResult(evt:ResultEvent):void
    {   
        employeeData = evt.result as ArrayCollection;
        Alert.show(employeeData.toString());
    }
}

First of all, evt.result is not an ArrayCollection, it is an Object (unless your SOAP service/WSDL are completely screwed up/malformed XML). 首先,evt.result 不是 ArrayCollection,它是一个Object(除非您的SOAP服务/ WSDL完全被破坏/ XML格式不正确)。

Second, you can't just display an Array or ArrayCollection (or generic Object, even) as a String (even though the .toString() method always seems to imply that) anyway, you have to parse the data to get what you want from it. 其次,无论如何,您都不能仅将Array或ArrayCollection(或什至是通用Object)显示为字符串(即使.toString()方法似乎总是暗示),您必须解析数据以获得所需的内容从中。

Now, the WebService class is nice in that it automatically parses the XML file that a SOAP service returns into a single usable Object. 现在,WebService类很不错,它可以自动将SOAP服务返回的XML文件解析为单个可用对象。 So that is actually the hard part. 所以这实际上是困难的部分。

What you need to do is call various properties of the object to get the data you need. 您需要做的是调用对象的各种属性以获取所需的数据。

So if the XML return (look at your WSDL to see what the return should be, I also highly suggest soapUI) is this: 因此,如果XML返回(请查看您的WSDL以查看应该返回什么,我也强烈建议soapUI)是这样的:

<employee name="Josh">
    <start date="89384938984"/>
    <photo url="photo.jpg"/>
</employee>

And you wanted to display "Josh" and the photo, you would do this. 您想显示“ Josh”和照片,就可以这样做。

var name:String = e.result.employee.name;
var url:String = e.result.employee.photo.url;

It does get more complicated. 它确实变得更加复杂。 If the WSDL allows for multiple nodes with the same name at the same level, it does return an ArrayCollection. 如果WSDL允许在同一级别相同名称的多个节点, 返回一个ArrayCollection。 Then you have to loop through the array and find the exact item you need. 然后,您必须遍历数组并找到所需的确切项目。

Just remember: The WSDL is god. 只需记住:WSDL是上帝。 Period. 期。 If it says there can be multiple "employee" nodes, you have to code accordingly, even if you don't see more than one in your tests. 如果它说可以有多个“雇员”节点,那么即使您在测试中看不到多个节点,也必须进行相应的编码。 The issue is that there always could be multiple nodes. 问题在于,总是可能存在多个节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM