Posts Tagged ‘Remote data commincation’

URL loader frowns “when HttpService does, why cant I ?”

May 19, 2009 4 comments

URL loader allows the users to handle requests and response to and from HttpServices same as what flex HttpService allows the users to do. So whatever the task can be done by HttpService, can also do by URL loader but the way they do will be different. We do have MXML HttpService tag which will invoke HttpServices like servlet in a much easier way than URL loader. Because we dont have MXML URL loader tag since its part of Flash player API so we can create an URL loader instance and call servlet using ActionScript alone.

To invoke a servlet using load method of URLLoader instance, the load method will expect a single parameter which is none other than URLRequest instance. It defines the URL in which it will request the servlet when URLLoader instance invokes the load method. As well as the event handler function for URLLoader complete event should be registered before you invoke the servlet because as you know well this event handler function will be executed once the request has been fulfilled.

var loader : URLLoader = new URLLoader();
var request : URLRequest = new URLRequest("http://localhost:8080/CallServlet/callService");

When we make a request to the servlet, we should also send request parameters which will be expected by the servlet when it receives the request. In URL loader we do have class named URLVariables through which we will create the request parameters as name/ value pair and finally we will set the data property of the URLRequest instance to URLVariables instance.

var param :URLVariables = new URLVariables(); = email.text;
param.pass = pass.text; = param;

Thats it. Now replace the URLLoader instance code, instead of HttpService instance code in our previous example & run the application.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="" layout="vertical" creationComplete="createLoader()">

 import mx.controls.*;

 var loader : URLLoader;
 var request : URLRequest;

 public function createLoader():void
 loader = new URLLoader();
 request = new URLRequest("http://localhost:8080/CallServlet/callService");
 public function callLoader():void
 var param :URLVariables = new URLVariables(); = email.text;
 param.pass = pass.text; = param;
 public function resultHandler(event : Event):void
 public function faultHandler(event : Event):void

 <!--<mx:HTTPService id="service" url="http://localhost:8080/CallServlet/callService" result="resultHandler(event)" fault="faultHandler(event)" method="GET" showBusyCursor="true" resultFormat="text">
 <mx:request xmlns="">
 </mx:HTTPService> -->

 <mx:Panel paddingLeft="20" paddingRight="20" paddingTop="20" paddingBottom="20">
 <mx:FormItem label="E-mail">
 <mx:TextInput id="email" />
 <mx:FormItem label="Password">
 <mx:TextInput id="pass" displayAsPassword="true" />
 <mx:Button label="Submit" click="callLoader()" />


Download previous example here

Note :

This post is continuity of my previous post if you could not follow this instructions. Please refer my previous post click here. Still you could not follow, drop your feedback i will try to refine the content as much as possible.