Advertise here

 
 
 
Total number of hits on the site :419   Monday, 21 Apr 2014
 
 

Advertisement

Adds by Siddhesh
Sidd Advertising
Puts your thought directly in customers brain.
Siddhesh Travels
Append wheels to your legs..get ready !
Siddhesh Rock's

MoQuotes




visitors by country counter
Country visit tracker


Like via Facebook

1) Flex and HTTPService PDF Print E-mail
Written by Administrator   
Monday, 11 April 2011 07:17

Flex provides three mechanisms to communicate with applications like Java/PHP.
They are
1) Http Service
2) Web Service
3) Remote object calling

Http Service is useful when you want to communicate with Servlet.
Remote object service is useful when you wan to to call POJO's available on server side.

Lets see how to use both methods one by one.

Using HTTPService
As we said earlier This method is usefull when you want to call servlet/jsp/PHP
With HTTPService Communication takec place in XML format. So we need to be careful
in presenting data in proper valid xml format.

Create a Dynamic Web project and create a servlet called First. DoGet method of this servlet should look like follows

protected void doGet(
final HttpServletRequest request, final HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
String userName = request.getParameter("userName");
String password = request.getParameter("password");
System.out.println("Got username and password " + userName + " " + password);

final PrintWriter pw = response.getWriter();
pw.println("<subject>");
pw.println("<list>");
pw.println("<name>Maths</name>");
pw.println("</list>");
pw.println("<list>");
pw.println("<name>Physics</name>");
pw.println("</list>");
pw.println("</subject>");
}

At client side we declare HTTPService like this in Declaration tag
<fx:Declarations>
<s:HTTPService id="subjectService"
 url="http://<ip>:<port>/<app-name>/First"
 result="subjectService_resultHandler(event)"
 fault="subjectService_faultHandler(event)"
 />
</fx:Declarations>  

Call to this service should be made in init() method of your flex app
public  function initApp():void
{
    subjectService.send();
}

Call this method on creationComplete so that this method gets called after you client app is loaded.
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx"
               creationComplete="initApp()">
 
We access the data in method subjectService_resultHandler() like follows

protected function subjectService_resultHandler(event:ResultEvent):void
{
var subjectList:ArrayCollection = event.result.subject.list
}

See, Data coming from server was in like this format
<subject>
<list>
<name>Maths</name>
</list>
<list>
<name>Physics</name>
</list>
</subject>

We added all "name" to an arrayCollection.
So now to access matches at client side we need to say subjectList[0]

Accessing strings is very easy. Suppose you have doGet Method with following xml string getting sent

<name>Siddhesh</name>
<designation>engg</designation>

protected function userService_resultHandler(event:ResultEvent):void
{
var userName:String = event.result.user.name;
var userDesig:String = event.result.user.designation;
}

Well, Now how to pass parameters to servlet

Lets see on click of a button yo want to send parameter "name" and  "password" to servlet.
So on click of button call a method say onClick(event:Event) whose content will be as follows

var params:Object = new Object();
params["uName"] = uNameInput.text;
params["uPassword"] = uPasswordInput.text;
userService.method = "POST";
userService.send(params);

And this call will go to doPost method of servlet First.
in doPost you call access these parameters using request object.

String uName = request.getParameter("uName");
String uPassword = request.getParameter("uPassword");

That it. Finally We need to add file called crossdomain.xml inorder to allow our Server to give access to our Flex client. crossdomain.xml file should be copied to root folder of your app and to web-content folder.


Following is content of crossdomain.xml file

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM
"http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<site-control permitted-cross-domain-policies="all"/>
<allow-access-from domain="*" to-ports="*" />
<allow-http-request-headers-from domain="*" headers="*" secure="false"/>
</cross-domain-policy>
 
Next tutorial will see how to use Remote object.

COMMENTS

avatar James cred
+1
 
 
Superb,. This is the most friendly and easy tutorials i have found. Thanks for sharing.
DATETIMEFORMAT
POST_REPLY
NAME *
EMAIL_VERIFICATIONS_REPLIES
CODE   
Thanks for your valuable Comment.
SUBMIT_COMMENT
Cancel
NAME *
EMAIL_VERIFICATIONS_REPLIES
CODE   
Thanks for your valuable Comment.
ChronoComments by Joomla Solutions
SUBMIT_COMMENT
Last Updated on Monday, 11 April 2011 08:45
 
 
Advertise here