Hi,
I am a newbie and I've been able to call Java methods when
they return a simple String. However, I am unable to get a
collection of Java objects. I get the following error message :
Type Coercion failed: cannot convert
mx.rpc.remoting.mxml::Operation@31717b1 to Array.
I have a datagrid in my application that has to be populated
by calling a method of a remote object.
The remote object (Java class looks like this).
public class PlayerService {
public PlayerService() {
}
public Player[] getPlayersAsArray() {
Player[] players = new Player[3];
Player rahul = new Player("Rahul",,"Dravid");
Player sachin = new Player("Sachin","Tendulkar");
Player saurav = new Player("Saurav","Ganguly");
players[0] = rahul;
players[1] = sachin;
players[2] = saurav;
return players;
}
}
I've configured the destination in my service-config.xml as
......
<destination id="PlayerService">
<properties>
<source>rpc.playerinfo.PlayerService</source>
<scope>application</scope>
</properties>
</destination>
.....
My mxml application code is given below :
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="
http://www.adobe.com/2006/mxml"layout="absolute">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.collections.ArrayCollection;
import rpc.playerinfo.Player;
import mx.utils.ArrayUtil;
[Bindable]
public var players:ArrayCollection;
public var myPlayer:Player;
]]>
</mx:Script>
<mx:RemoteObject
id="playMe"
destination="PlayerService"
showBusyCursor="true">
<mx:method name="getPlayersAsArray">
<mx:arguments>
</mx:arguments>
</mx:method>
</mx:RemoteObject>
<mx:ArrayCollection id="playerList"
source="{playMe.getPlayersAsArray}"/>
<mx:DataGrid dataProvider="playerList" width="100%">
<mx:columns>
<mx:DataGridColumn dataField="FirstName"
headerText="FirstName"/>
<mx:DataGridColumn dataField="LastName"
headerText="LastName"/>
</mx:columns>
</mx:DataGrid>
</mx:Application>
One question I have is, how does Flex know that what's
returned is an array of Player objects. I did define the Player in
an Action script file.
package rpc.playerinfo
{
[RemoteClass(alias="rpc.playerinfo.Player")]
[Bindable]
public class Player
{
public var firstName:String;
public var lastName:String;
public function Player() {}
}
}
but it appears that I have never actually used it in the mxml
file.
Thank you very much.