Rocker
Rocker

Reputation: 681

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]. Can anybody help me on this.

Below is the code for my web service:

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());
    }
}

Upvotes: 0

Views: 484

Answers (1)

Josh
Josh

Reputation: 8149

First of all, evt.result is not an ArrayCollection, it is an Object (unless your SOAP service/WSDL are completely screwed up/malformed 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.

Now, the WebService class is nice in that it automatically parses the XML file that a SOAP service returns into a single usable Object. 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:

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

And you wanted to display "Josh" and the photo, you would do this.

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. Then you have to loop through the array and find the exact item you need.

Just remember: The WSDL is god. 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.

Upvotes: 3

Related Questions