5 Replies Latest reply on Aug 22, 2009 2:45 AM by Ansury

    Sending data to Java backend

    asagohan

      What is the best way to send a flex object to a java backend and then operate on it on the server? I have an array that holds a custom class. When the user clicks submit, I want to send the array to Java and then get the contents of the array manipulate the contents and write it to a file.

       

      I have a remote Java Object that I would like to send the data to, in order to manipulate it. I don't think I need a remote object to represent the flex object though because that would mean that whatever changes I make to the object in flex continually gets sent to the backend right? I just want to send it all in one go after the user has finished editing it on the front end.

       

      I have found XML serializers that people have made which recursively go through the object and create XML. Then it has to be deserialized on the Java side. I thought that there must be a simpler way. Is there a library or something for this?

        • 1. Re: Sending data to Java backend
          Ratsnackbar Level 2

          Hi:

           

               A RemoteObject would still work.  Remote Method calls do not need to have continuous updates.  You would simply need to trigger sending the Object to your server based on an event or button click.  I do it all the time with ColdFusion and Java is not that much different.

           

               I would suggest RemoteObjects over XML as the AMF3 protocol is much much faster.

           

               http://livedocs.adobe.com/flex/3/html/help.html?content=data_access_4.html

           

          -Joe

          1 person found this helpful
          • 2. Re: Sending data to Java backend
            asagohan Level 1

            Thankyou for your reply. I am still not sure if that is what I want though. Because I will have an array, which holds a bunch of canvas objects, which hold a bunch of text objects.


            I am not sure how I would go about getting that to the server. Do I have to create a remote object for the array of canvases, remote objects to represent each canvas and remote objects for each piece of text? Potentially creating hundreds of remote objects. When I create the array remote object, I create multiple canvas remote objects and put them in the array, then add each of my text remote objects to each of the canvases? Is that how it works?

             

            I tried this but I keep getting an error:

             

            (mx.rpc::Fault)#0
              content = (null)
              errorID = 0
              faultCode = "Server.ResourceUnavailable"
              faultDetail = "The expected argument types are (example.MyClass) but the supplied types were (flex.messaging.io.amf.ASObject) and converted to (null)."
              faultString = "Cannot invoke method 'addItem'."
              message = "faultCode:Server.ResourceUnavailable faultString:'Cannot invoke method 'addItem'.' faultDetail:'The expected argument types are (example.MyClass) but the supplied types were (flex.messaging.io.amf.ASObject) and converted to (null).'"

             


            This is the java code:

             


                 HelloWorld.java

             

            package example;
            
            import java.util.ArrayList;
            
            public class HelloWorld
            {
                 ArrayList <MyClass> x = new ArrayList();
                 public HelloWorld()
                 {
                 }     
                 public void addItem(MyClass myClass)
                 {
                      x.add(myClass);
                 }
                 
                 public String getHelloWorld()
                 {
                      String concatenation = "<";
                      for(MyClass myClass:x)
                      {
                           concatenation += myClass.toString();
                      }
                      concatenation += ">";
                      return concatenation;
                 }     
                 
            }
            

             


                 MyClass.java

             

            package example;
            
            public class MyClass {
            
            }
            

             

             

             

            Flex code:

             


                 helloworld.mxml

             

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
                     <mx:Script>
            
                <![CDATA[
                     import mx.rpc.remoting.RemoteObject;
            
                import mx.rpc.events.ResultEvent;
            
                import mx.rpc.events.FaultEvent;
            
                import mx.utils.ObjectUtil;
            
                import mx.controls.Alert;
            
                import mx.utils.StringUtil;
            
                private function resultHandler(event:ResultEvent):void
            
                {
            
                //used for debugging - shows details about result
            
                //returned by the Java class method
            
                Alert.show( ObjectUtil.toString(event.result) );
            
                }
            
                private function faultHandler(event:FaultEvent):void
            
                {
            
                Alert.show( ObjectUtil.toString(event.fault) );
            
                }
            
            
                 private function makeABunchOfObjects():void
                 {
                     var myClass:RemoteObject = new RemoteObject();
                     myClass.destination = "helloworld2";
                     myClass.source = "example.MyClass";
                    
                       var helloWorldRO:RemoteObject = new RemoteObject();
                     helloWorldRO.destination = "helloworld";
                     helloWorldRO.source = "example.HelloWorld";
                     helloWorldRO.addEventListener("result", resultHandler);
                     helloWorldRO.addEventListener("fault", faultHandler);
                     helloWorldRO.addItem(myClass);
                     helloWorldRO.getHelloWorld();       
                 }
                ]]>
            
                </mx:Script>
            
                <mx:Panel x="10" y="10" width="440" height="200" layout="vertical" title="Test Flex 3 Using Java" borderColor="#008040" fontFamily="Arial" fontWeight="bold" fontSize="13">
            
                <mx:Text text="Click The Button To Test Getting Data Into Flex From A Java Class" fontWeight="bold" width="250"/>
            
                <mx:Spacer height="20"/>
            
                <mx:Button label="Get Hello World!" click="makeABunchOfObjects()"/>
            
                </mx:Panel>
            
            </mx:Application>
            
            
            • 3. Re: Sending data to Java backend
              Ratsnackbar Level 2

              Perhaps I am not understanding the reason for your implementation so do not take this personally.  But why would you want to send a Canvas object which is part of the view to the server?  If you are only wanting to send the data contained within the text objects to the server for processing what you would want to do is to create a ValueObject (TransferObject to some) and send that to the server along with perhaps any instructions you would like to send.

               

              On the other hand (and this is just a blind guess at what you are trying) if you are wanting to send the objects so that you can implement some sort of server side compilation, then you would want to send the raw text object for the canvas as one of the parameters contained within your ValueObject.  Either that or you could use Adobe AIR and zip the entire object set and send the file to the server where it can be decompressed and compiled.

               

              Here is a blog article on transfering data between Java and Flex using RemoteClass.

               

              http://pachamuthu.wordpress.com/2009/07/15/using-blazeds-to-send-user-defined-data-types-d ata-transfer-objects-from-java-to-flex/

               

              Hope this helps.  If not then perhaps a little more information on what you are attempting to accomplish might help.

               

              -Joe

              • 4. Re: Sending data to Java backend
                asagohan Level 1

                Hi Joe,

                 

                Sorry, I did not explain it properly. I just need a DTO to be sent to Java. Which I think I have working now. However there are still a one thing that I don' quite understand.

                 

                I have the following calls in flex my remote object:

                 

                         var myClass:MyClass = new MyClass();
                         var myClass2:MyClass = new MyClass();
                         
                         var helloWorldRO:RemoteObject = new RemoteObject();
                         helloWorldRO.destination = "helloworld";
                         helloWorldRO.source = "example.HelloWorld";
                         helloWorldRO.addEventListener("result", resultHandler);
                         helloWorldRO.addEventListener("fault", faultHandler);       
                         myClass.x = "blah";
                         helloWorldRO.addItem(myClass);
                         myClass2.x = "2BLAH";
                         helloWorldRO.addItem(myClass2);
                         helloWorldRO.getHelloWorld();   
                

                However, it seems like

                helloWorldRO.getHelloWorld(); 
                

                is getting executed before

                        helloWorldRO.addItem(myClass);

                and

                helloWorldRO.addItem(myClass);

                how can this be??

                 

                Here is the Java:

                 

                package example;
                
                import java.util.ArrayList;
                
                public class HelloWorld
                {
                    ArrayList <MyClass> x = new ArrayList<MyClass>();
                    public HelloWorld()
                    {
                    }    
                    public void addItem(MyClass myClass)
                    {
                        x.add(myClass);
                    }
                    
                    public String getHelloWorld()
                    {
                        String concatenation = "<";
                        for(MyClass myClass:x)
                        {
                            concatenation += myClass.x;
                        }
                        concatenation += ">";
                        return concatenation;
                    }    
                    
                }
                
                
                

                 

                The result I get is

                "<>".

                Then I get "(null)"

                and the n I get "(null)"

                 

                Instead of:

                "(null)"

                "(null)"

                "<>"

                • 5. Re: Sending data to Java backend
                  Ansury Level 3

                  I saw XML mentioned and have to add: Don't even think of using XML for data transfer.  I'd sooner switch careers and become a mall security guard.

                   

                  Also, RemoteObject doesn't do anything magic-- it doesn't automagically sync Java objects up with what's on the Flex side.  You have to call the remote methods from Flex, usually in what's called a "Delegate".