Thursday 14 April 2011

Flex SMS- Application using Http-Services.


I have created a small application in flex that sends SMS from flex to mobile. i have created this application by using Flex-Webservices,so first i will explain  briefly about webservices.

Flex Remoting services:-

           Flex remoting services are used to make following

                       1. Http calls to webservices
                       2. Webservices->Call Soap+Wsdl based web-services.
                       3. Remoting ->Call remote object services such as Coldfusion (or) Java.

HTTP - service is used to make Http request  and handle results.When you call Http service object's "send()" method,it makes a http request to specified "url" and Http response is returned(asyncronously). Every Http call returns a token call as "Asynctoken".The Http call results either result (or) fault,this can be handle by using "Responder". In flex we can Http-services either from MXML or from Action script.


Calling Http -services  in Flex MXML Component:-

In Flex we have " <mx:HTTPService>" tag to use Http-services.

Example:-          <mx:HTTPServices  id ="httpservice"

                             url="Your webservice url... to make request"

                             method="Get"

                             requestformat = "Text"

                            result ="resultHandler(event)"

                            fault = "faultHandler(event)"    / >




Calling Http- services in Action Script:-

 To call Http-services we need to import following

                import mx.rpc.http.HTTPService;
           
                import mx.rpc.events.ResultEvent;
            
                import mx.rpc.events.FaultEvent;

sample code:-

                var http: HTTPService = new HTTPService();

                http.url=" you webservice url.........";

                http.resultFormat = "text";
      
                http.method = "POST";
               
                http.addEventListener("result", httpResult);
               
                http.addEventListener("fault", httpFault);
                
               http.send();

  For sending Sms from flex to mobile we need to have SMS gateways that provides Api services  to make requests, my colleague charan explored on that he find many Sms-gateways providing Api servies to send sms. i haves  used following  gate ways,

                                            Sms Global   and Clickatell.


Now Creating Application:-


 Step 1:-  Creating Project in Flex3

                   Goto File-->New-->Flex Project

                   Name it as "SMS Demo App"  and Select Application type as "Web Application" and click on finish.

 Step 2:-  Goto  your project in project navigation window--> Click on src folder--> Double click on ProjectName.MXML component. Then in developing window Mxml component is opened.Now copy the following code and paste in Mxml component.


project name.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
 <mx:Script>
 <![CDATA[
   import mx.rpc.http.HTTPService;

   import mx.controls.Text;
 
   import mx.controls.Alert;

   import mx.rpc.http.HTTPService;
        
   import mx.rpc.events.ResultEvent;
          
   import mx.rpc.events.FaultEvent;

   private var Fname:String;
 
   private var Phone:Number;
 
   private var msg:String;
 
   private var sUsername:String = "******";// user name of sms global or clickatell.
 
   private var sPassword:int=*******;// password of smsglobal or clickate.
 
   private var sId:String="Flex testing Sms";
 
 function send():void
 {
 
       Phone = Number(con.text);
    
       msg = inputmsg.text;
          
      Alert.show("phone number: "+Phone+"\nmessage: "+msg);
    
      var http: HTTPService = new HTTPService();
    
      http.url =" http://api.clickatell.com/http/sendmsg?user=hari****&password=*****&api_id=****&to="+Phone+"&text="+msg;  // URL to send Http requests....
    
    
     // http.url  = "http://www.smsglobal.com/http-api.php?action=sendsms&user="+sUsername+'&password='+sPassword+'&from='+sId+'&to='+Phone+'&text='+msg;  //  SMS  Global  Url to send Http requests....
 
      http.resultFormat = "text";
    
      http.method = "POST";
            
      http.addEventListener("result", httpResult);
            
      http.addEventListener("fault", httpFault);
              
      http.send();
    
     }
     public function httpResult(event:ResultEvent):void {
                var result:Object = event.result;
            //Do something with the result.
            }

    public function httpFault(event:FaultEvent):void {
                var faultstring:String = event.fault.faultString;
                Alert.show(faultstring);
              
    }

  ]]>
 </mx:Script>

 <mx:Form id="smsform" label="Sample Sms App">
            <mx:FormItem label="To(Phone Number)" required="true">
                <mx:TextInput id="con"  maxChars="12"/>
            </mx:FormItem>
            <mx:FormItem  label="MessageBody" required="true">
                <mx:TextArea id="inputmsg" />
            </mx:FormItem>
            
      <mx:Button label="Send" id="myButton" click="send()" />

 </mx:Form>

</mx:Application>

        Now you get form...

    
Note:- first you need to create Accounts @ Sms gate ways to use services.After you have created account you can get username and password.

No comments:

Post a Comment