5 Replies Latest reply on Nov 8, 2006 7:16 AM by maxrahder

    Newbie: Sending data to REST-style service

    maxrahder
      I am interested in sending/receiving XML data from Flex to a Java-based Web application. What should I read to get up to speed on the Flex techniques to do this? Are there any examples that send/receive XML data?

      Here's the background: I'm in the process of coding a MVC Web application (Tomcat, Java, servlets, JSP) that uses the controller to fetch XML that is then viewed by JSPs. I had planned on passing the XML as parsed DOM objects using something like JDom. But someone recently showed me Flex 2, so after saying "wow" I started thinking "Hmmm, why not change the Web app to act like a REST service, sending and receiving the XML to the Flex client." I played around with Flex a little, and see that I can use HttpService to fetch such data. But I'm vague on how to allow the user to edit XML elements and send such changes back to the server. In the short-term I'll use this knowledge to construct the Web application in a way that'll be compatible with what Flex needs. Then, as I learn more about Flex, I'll start using Flex to front-end the data.

      I've seen an example that uses mx:RemoteObject to send data. However, my data isn't represented by a Java class with getters/setters. And I don't want to bother doing that. Instead, I'd rather the servlet simply send XML, and receive a copy of the same XML with user-updated values. (The server will simply post the XLM to the datastore).

      I know practically nothing! So feel free to recommend some pretty fundamental readings. (I've seen that the Adobe Flex site has lots of examples, white papers, tutorials, etc. -- there are just so many options I don't know where to start to address my question.)

      Thanks for any and all advice!
        • 1. Newbie: Sending data to REST-style service
          Chris River
          I'm pretty much in the same boat as you, just getting around to learning Flex. I spent the better part of a day trying to figure this out. The following is what I ended up with. I believe it's pretty close to what you are looking for:

          First I defined the parameters (in this case I'm using XML):
          var params:XML = new XML(<myXML>meh</myXML>);

          then create a new HTTPSevice
          var service:HTTPService = new HTTPService();

          then set its atttrubutes accordingly
          service.url = "url://to.your.service.name"
          service.method = "post";
          service.contentType = "application/xml";
          service.resultFormat = "xml";


          add a listener so we know when to parse results
          service.addEventListener("result",httpResult);

          then build the httpResult function that handles the result..

          pretty basic, but I hope it helps.
          • 2. Re: Newbie: Sending data to REST-style service
            ntsiii Level 3
            Yep, that is how I would do it, though you can create the HTTPService in mxml if you want.

            Also, I have heard of folks having trouble when they attempt to set the contentType. I have not tried it myself, since i have been creating my own XMLHttp sources, and have just sent my xml as a string.

            Tracy
            • 3. Re: Newbie: Sending data to REST-style service
              maxrahder Level 1
              I can't tell if that answers my question or not! I'll study it.

              Again, I'm trying to SEND data from the browser TO the server. In HTML terms this would mean sending query data via a post or get. I have seen HTTPService examples of simply firing off a URL to receive a result, but I have NOT seen an example of actually building XML data on the Flex client side and somehow sending it to the server.

              If your example I see where you define local XML in "params", but I don't see how that XML is sent to the server.
              • 4. Re: Newbie: Sending data to REST-style service
                peterent Level 2
                I have an example of using REST from a Flex application that communicates with Flickr. You are welcome to download the code and pick it apart:

                Flickr Photo Search

                Once the app has started, right click the mouse anywhere over it and select "View Source". The REST code is in the com.adobe.flickr.Flickr class.

                At the bottom left corner of the View Source page you'll see a link to download the entire source.

                Disclaimer: I am not a developer so don't judge the code too harshly :-) The code is not supported and use it at your own risk.
                • 5. Re: Newbie: Sending data to REST-style service
                  maxrahder Level 1
                  I tried the app. It's great! I'll study the source. Thanks for the help!