File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Flex and the fly likes Question for Flex Authors (or others) on Web Service Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Flex
Bookmark "Question for Flex Authors (or others) on Web Service" Watch "Question for Flex Authors (or others) on Web Service" New topic
Author

Question for Flex Authors (or others) on Web Service

Al Johnston
Ranch Hand

Joined: May 02, 2009
Posts: 99

Hi Bernard and Jeremy,

I created a Java server which I used to run as an RMI service that talked to ColdFusion and then delivered data from ColdFusion to Flash in XML. We moved to Flex in Flash Builder 4 and decided to drop CF and make a web-service. In reading about Flex, it seemed great. I can return a List<Map> from Java (via a web-service) and then the List will be an ArrayList in Flex where the Maps will be Objects and on each Object I can get the value by using the Java key. For instance:



Now, in Flex, I should be able to do this:



I can do the above and have success. What is strange to me is the return (when I don't get an Apache error telling me that it can't create a mapping for ArrayList and I actually get a return anyway). To continue, now that my service is bound and I've gotten data back to my result handler, I should be able to do the following according to everything I read about ActionScript and Flex:



Are there any pointers you can give and/or would your book help me fix this? In order to get Maps or Lists that are usable by my ActionScript/Flex code, I have to do a lot of looping and printing to screen which is killing me in development and is not creating optimized code. What good does it do me to return an Object if I have to do exponentially more times the amount of processing than I would have to do if we just stayed with XML and JSON? Surely I am doing something wrong...
Al Johnston
Ranch Hand

Joined: May 02, 2009
Posts: 99

BTW, My sample code for getting returned values is not entirely correct. It needs one more inner loop to loop the number of keys and values stored in the object. In real life, the only way I've been able to make it work is to loop the ArrayCollection (which is really an ArrayList), get each object and then loop the object "for (var key:String in Object)" as an inner loop. Then for each "key" in "Object" get the "key" and then get the "value" such that the final result is var returnedValue = String (ArrayCollection.getItemAt(location).k["value"]. I need loops of loops to get collection values back which defeats the purpose of sending back maps at all. If one of my values of the map happens to be a Map, I wind up with loops building on top of loops just to get to the point of having usable Flex data.

Also, the first result handler you see in my post (the second code block) was put together when I was surprised by the first web-service call we tried. That call does not return a List. It returns a Map. I tried var:myValue = returned_map.server_key and got null pointers. I put in the loop to find out that my keys were all stored under "key" keys and my values were all stored under "value" keys. I was able to get the actual values I need through the loop. But, I should be able to get the values I need without any looping at all.
Jeremy Anderson
author
Ranch Hand

Joined: Apr 26, 2005
Posts: 32
I tend to avoid using the Java Collections classes when doing SOAP web services as they're not really recommended per the w3c recommendations. To see the real benefits of using objects, you may want to look into using BlazeDS rather than SOAP.
Al Johnston
Ranch Hand

Joined: May 02, 2009
Posts: 99

Thanks Jeremy. We are trying to build something that works with every front end, not just Flex and thus built a standard web-service. It looks like the simplest means to getting this done is to go ahead and put Blaze DS in as yet another framework.

I read the first chapter. I thought it did a great job of explaining the concept, giving step by step examples with comprehensive information - without being verbose. It would have been a great resource to have a few weeks ago when I started learning Flex. The sample chapter is way better than any resource I was using to learning as it is so Java focused. Explaining Maven and Spring (both of which we use on this project) was fantastic.
Jeremy Anderson
author
Ranch Hand

Joined: Apr 26, 2005
Posts: 32
Exposing your services using BlazeDS and SOAP are no longer mutually exclusive. There's no reason you can use those same services for both, these days it's just a matter of adding another annotation to your service methods. That way you can expose them as SOAP web services for the clients that want to use SOAP, and BlazeDS services for your Flex clients.
Al Johnston
Ranch Hand

Joined: May 02, 2009
Posts: 99

Makes sense. I am not the web-services guy. Have never done anything but call them. I wrote the code that goes on either side (the UI and the Server). As we got into using Flex, we had these discussions and tried to pick what we though would be most re-usable. Now that I've gotten BlazeDS up and experienced how it works, I can totally see how you can accomplish both tasks at the same time with one object.

As an aside for anyone who may stumble upon this and have the same trouble I did... When setting up BlazeDS using Maven instead of setting up per the instructions on Adobe, I had to add the apache-commons-http and logging dependency to the pom to get MessageBrokerServlet to instantiate.
Al Johnston
Ranch Hand

Joined: May 02, 2009
Posts: 99

BTW, now that we've gotten into Flex and are quickly adopting it, I am still a little lost as to why we went this way. I like the familiarity of ActionScript and the ease of MXML, but if you don't mind me getting off topic, what is the advantage of Flash over Java FX and/or Swing and Applets or even Swing and JNLP?

I have found that in order to use Flex, I have to have some helpers from time to time - whether the helper be JavaScript or some other helper. For instance, I wanted to automate an upload process where we need to ensure certain directories have certain things before we upload. Since Flash runs in a security sand-box, I found that what we were trying to do would not work. The only way to make it work is to use JavaScript to browse directories and build logic and then validate it all in Flex and then call back to JavaScript to do more work then back to flex... or use a technology that can be configured to allow directory browsing. So, I wound up writing a JFileChooser and JTable with a table-model and a JNLP file for web-start for this one exception process. As I did that, I wondered why we did not just make the whole thing Java since we all know it and it seems to be able to do anything that Flex can do plus not have security restrictions. Don't get me wrong... I like Flex and am comfortable with it. Just curious why you would select it over arguably more capable technologies like Java (especially now that there are layout tools in the IDE for alternatives to Flex).

Maybe I should make this a new thread. Apart from your thoughts on the advantages of Flash over other solutions, does your book discuss where Flex is going and do you believe that html 5 or any other technology is likely to change the relevance of Flash?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Question for Flex Authors (or others) on Web Service
 
Similar Threads
Problem consuming WebService in Flex 3
Can't Get BlazeDS return results to be ArrayList in Flex
DataGrid With RemoteObject
examples of java and flex combined
Flex and Java cannot add anything to list