Flex and Rails : Send – Receive JSON/XML/AMF data

Surendran Sukumaran

2 min read

If you want to know how to send and receive data from Flex and Rails using XML or JSON or AMF data types, then please read further.
Firstly, to get a bite at of what i am talking about, please check the demo.
Now let see this from coding perspective.

  • Create a simple rails app with a simple scaffold named tasks with name(String) attribute.
  • Create a simple flex apps to display all the tasks. I used Gridview to list tasks and kept the grid inside a panel.

Here is the flex part of the code-snippet for XML, JSON and AMF.

XML

[source language=”xml”]




[/source]
on creationComplete call the HTTPService as “index.send()”
In the ablove http service, we are requesting for xml data ,once the request hits the rails apps that is, Task controller index action, the rails app returns xml data.The Rails code goes below.
[source language=”ruby”]
#Rails
def index
@tasks = Task.find.all
respond_to do |format|
format.xml { render :xml => @tasks }
end
end
[/source]
Then we handle the result in flex with the method index_resultHandler and assgin the result to an arraycollection.
[source language=”xml”]
#Flex
private function index_resultHandler(event:ResultEvent):void
{
tasks = event.result.tasks.task
}



[/source]

JSON

[source language=”xml”]




[/source]
on creationComplete call the HTTPService as “index.send()”
In the above http service, we are requesting for json data ,once the request hits the rails apps that is, Task controller index action, the rails app returns json data.The Rails code goes below.
[source language=”ruby”]
#Rails
def index
@tasks = Task.find.all
respond_to do |format|
format.json { render :json => @tasks }
end
end
[/source]
[source language=”xml”]

import com.adobe.serialization.json.JSON;
[/source]
Then we handle the result in the below method , decoding json response using JSON.decode(), assing it to an arraycollection. This is how the flex code looks .
[source language=”xml”]
protected function index_resultHandler(event:ResultEvent):void
{
var json:Object = JSON.decode(event.result as String);
tasks = new ArrayCollection(json as Array);
}



[/source]

AMF(Action Message Format )

AMF is a binary format used to serialize ActionScript objects.
It is used primarily to exchange data between an Adobe Flash application and a remote service.
Flex Remote service connects to a specific “gateway” URL on a web server, accesses the service which handles AMF communication.
[source language=”xml”]




[/source]
on creationComplete call the remote service that is RubyAMF gateway like “taskService.index()” .
We use RubyAMF plugin to respond to AMF request.
Install RubyAMF pluginin your rails apps using th below command.
ruby script/plugin install http://rubyamf.googlecode.com/svn/tags/current/rubyamf
RubyAMF redirects to appropriate controller and action , in action return amf data like the below
To know more about integrating RubyAMF with rails apps clickhere
The below rails code show how to respond to AMF request.
[source language=”ruby”]
def index
@tasks = Task.find.all
respond_to do |format|
format.amf { render :amf => @tasks }
end
end
[/source]
Handle the result in a method and assign it to an Arraycollection and bind the array collection to the datagrid and the code looks like this.
[source language=”xml”]
protected function taskService_result_handler(event:Object):void
{
var re:ResultEvent = event as ResultEvent;
tasks_amf = re.result as ArrayCollection;
}


[/source]
Hope you like it.
If you want more info on Rubyamf with rails, please vist blog
Thanks to RubyAMF for a excellent plugin to hook Rails with Flex.
Do you want the complete source code, please drop a comment, and i will post it in github.