Monday, April 30, 2007

Communication over HTTP in FLEX 2.

So, it seems that things are not that easy with FLEX2.0 after all.
We need to write a piece of code that sends some ( XML ?) message through an HTTP request to some servlet, get the HTTP response and extract the reply XML message from the response.

First, create an instance of the URLLoader class, The URLLoader class downloads data from a URL as text (default), binary data, or URL-encoded variables.
var requestSender:URLLoader = new URLLoader();

Second, add event handler for the complete event of the loader class:
requestSender.addEventListener(Event.COMPLETE, completeHandler);

We then have to create a URLRequest instance, taking the URL of the servlet. The URLRequest class captures all of the information in a single HTTP request.

var urlRequest :URLRequest = new URLRequest("serverUrl");


Next, we create the message to be sent and put it in the "data" property of the URLRequest instance created above, set the value of the URLRequest.method property to URLRequestMethod.POST.

var msg:String = “valid XML msg” ;
urlRequest.data = msg;
urlRequest.set_method("POST");


After that, the request object is then passed to URLLoader.load(), which does the HTTP request and posts the message:
requestSender.load(urlRequest);

When the URLLoader has finished loading the text file the Event.COMPLETE event fires, triggering the completeHandler() method.
function completeHandler(event:Event)
{
var response:URLLoader = URLLoader(event.target);
String responseData = this.response.data;
}

Note that the reply XML message is obtained from the URLoader.data property on the instance obtained from the event.target.
We now have that working piece of code:

var requestSender:URLLoader = new URLLoader();
var urlRequest :URLRequest = new URLRequest("serverUrl");
var msg:String = “valid XML msg” ;

requestSender.addEventListener(Event.COMPLETE, completeHandler);
urlRequest.data = msg;
urlRequest.set_method("POST");
requestSender.load(urlRequest);

function completeHandler(event:Event)
{
var response:URLLoader = URLLoader(event.target);
String responseData = this.response.data;
}

As you might already have guessed, the responseData can be then used to construct an XMLDocument instance, where you can perform different XML parsing operations.

4 comments:

Unknown said...

I'm able to send information to a servlet, but I'm not sure how to set the response back to flash. I'm not very experienced with Java. Any idea?

luckyboy said...

Here's the main servlet method :
public void service (HttpServletRequest request, HttpServletResponse response) {
}

You use the request param to get the message sent in the HTTP request, do whatever logic you want to do with it, construct your reply and send it via the response parameter. Do the reply by getting the output stream and writing to it using the blocking "write" method. You should be doing something like this :

response.getOutputStream().write(responseMessageInBytes);

Don't forget to set some HTTP headers as appropriate(for example the content length and the HTTP status code).

Unknown said...
This comment has been removed by the author.
Hamdy K. said...

You should also set the content type while setting the data
urlRequest.ContentType = "text/html"