3 Replies Latest reply on Sep 15, 2006 2:07 PM by arkcto

    passing an Array Remote Object

    arkcto
      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.
        • 1. Re: passing an Array Remote Object
          T._Ruggles Level 1
          You need to actually call the method (playMe.getPlayersAsArray()) and your binding should be to {playMe.getPlayersAsArray.lastResult}.

          HTH,
          Tom Ruggles
          FDS QA
          • 2. Re: passing an Array Remote Object
            JSSanders
            On your method in remote object there is a result attribute. Create a function to handle that result and then get event.result back as an arrayCollection.

            In your instance you dont 'have' to actually create a player object for each item that is returned in the array. you can just bind the results to the grid.

            Here is some sample code:
            <mx:RemoteObject id="playMe" destination="PlayerService"
            showBusyCursor="true">
            <mx:method name="getPlayersAsArray" result="handlePlayersResult(event.result as ArrayCollection)"/>
            <mx:arguments>
            </mx:arguments>
            </mx:method>
            </mx:RemoteObject>


            Then down in your script object create a Bindable variable like this:

            [BINDABLE]
            public var playerList;

            then a result handler function like this:
            public function handlePlayersResult( event: ArrayCollection)
            {
            //then set the event result which is being sent into this function to the playerList //var
            playerList = event;
            }

            Assuming your grid is set to that dataprovider of {playerList} and on application crationComplete you call the remoteObject mentioned above with that result handler it should work.
            • 3. Re: passing an Array Remote Object
              arkcto Level 1
              Thanks guys.

              JS, I got what you are saying. Now I don't have any error messages, but the data grid is blank.

              This is the mxml code. (I added your input)

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml">

              <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 function handlePlayersResult(event: ArrayCollection)
              {
              //then set the event result which is being sent into this function to the playerList
              players = event;
              }

              ]]>
              </mx:Script>

              <mx:RemoteObject id="playMe" destination="PlayerService"
              showBusyCursor="true">
              <mx:method name="getPlayersAsArray" result="handlePlayersResult(event.result as ArrayCollection)"/>
              </mx:RemoteObject>

              <mx:DataGrid dataProvider="{players}" width="100%" creationComplete="playMe.getPlayersAsArray()">
              <mx:columns>
              <mx:DataGridColumn dataField="firstName" headerText="FirstName"/>
              <mx:DataGridColumn dataField="lastName" headerText="LastName"/>
              </mx:columns>
              </mx:DataGrid>

              </mx:Application>

              What is it missing ?

              Thank you so much.