Calling Webservices in Flash Using AS 3.0

Flash and Asp.net, C# Relation

Calling Webservices in Flash Using AS 3.0

Postby Ashish on Thu Mar 31, 2011 3:01 pm

Code: Select all
import alducente.services.WebService;
import flash.events.*;
import flash.net.URLLoader
import flash.net.URLRequest

//find the attached .zip and keep it near your fla file
// Make a button on stage and give it instance name 'btn'
btn.addEventListener(MouseEvent.CLICK, callWS)

function callWS(e:MouseEvent){
    passDataToWs('param1Value', 'param2Value', 'param3Value')
}



function passDataToWs( param1, param2, param3){
           var ws:WebService = new WebService();
      ws.addEventListener(Event.CONNECT, connected);
      ws.connect("http://provideWebserviceUrlHere?WSDL");
      ws.cacheResults = true;
   var initTime:Number;

   
   function connected(evt:Event):void{
      initTime = getTimer();
      ws.METHOD_NAME(done, param1, param2, param3);
      ws.METHOD_NAME(done2)
   }

   function done(serviceRespone:XML):void{
      trace("\nWeb Service Result: ");
      var time:Number = getTimer();
      trace("Call duration: "+(time - initTime)+" milliseconds");
      initTime = time;
   }
   function done2(serviceRes:XML):void {
      ws.clearCache();
      trace("\nCache was cleared");
   }
   
}



Take the following steps to resolve the Error #2044: Unhandled securityError:. text=Error #2048: Security sandbox violation


*If your SWF file location and webservice location both are on different domain then,
Please put and upload the following xml code in a file named 'crossdomain.xml' on the location of where your webservice is hosted.

More Info on CrossDomain.xml


Code: Select all
<?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="master-only" />
   <allow-access-from domain="WebserviceIsHostedDomain.com" />
   <allow-http-request-headers-from domain="*" headers="*" secure="false"/>
</cross-domain-policy>



Use FlashFireBug to see the game behind the curtain (requests from flash to webservice).

All The Best...

Bookmark and Share
Attachments
alducente.zip
(7.89 KiB) Downloaded 193 times
Ashish
 
Posts: 153
Joined: Tue Oct 14, 2008 1:49 pm

Re: Calling Webservices in Flash Using AS 3.0

Postby sandarya.info on Wed Apr 27, 2011 10:49 am

I am fresher in action script and in my first assignment i am facing web service, i have to send some string value (you can see the method and string values on the link itself)to web service, so that they can be store in the database.

i need some more explanation below is the code and i have commented(They are in red) where i need explanation, i am screwed please help.

My webserviceURL :- http://webservice url?

How can i set string values according to above web-service link ?

[action script codes 3.0]

import alducente.services.WebService;
import flash.events.*;
import flash.net.URLLoader
import flash.net.URLRequest

function tiListener(evt_obj:Event){
if (tpassword2.text != tpassword1.text || tpassword2.length < 4) {
trace("Password is incorrect. Please re-enter it.");
}
else {
var password2:String = tpassword2.text;
var password1:String = tpassword1.text;
var uname:String = tuname.text;
var mail:String = tmail.text;
var age:String = tage.text;
var country:String = tcountry.text;

//trace(uname);
//trace(mail);
//trace(password1);
//trace(password2);
//trace(age);
//trace(country);
}
}



Submit.addEventListener(MouseEvent.CLICK, callWS)

function callWS(e:MouseEvent){
passDataToWs('uname', 'mail', 'password1', 'password2', 'age', 'country')
}



function passDataToWs(uname, mail, password1, password2, age, country){
var ws:WebService = new WebService();
ws.addEventListener(Event.CONNECT, connected);
ws.connect("http://provideWebserviceUrlHere?WSDL");
ws.cacheResults = true;
var initTime:Number;


function connected(evt:Event):void{
initTime = getTimer();
ws.METHOD_NAME(done, uname, mail, password1, password2, age, country);
ws.METHOD_NAME(done2)
//what is "done", "done2" refers for
}

function done(serviceRespone:XML):void{
trace("\nWeb Service Result: ");
var time:Number = getTimer();
trace("Call duration: "+(time - initTime)+" milliseconds");
initTime = time;
}
function done2(serviceRes:XML):void {
ws.clearCache();
trace("\nCache was cleared");
}

}

Thanks in advance

Bookmark and Share
Last edited by sandarya.info on Wed Apr 27, 2011 1:19 pm, edited 1 time in total.
sandarya.info
 
Posts: 3
Joined: Tue Apr 26, 2011 8:47 pm

Re: Calling Webservices in Flash Using AS 3.0

Postby Ashish on Wed Apr 27, 2011 12:37 pm

Dear Sandy

The url provided by you (http://xxx.xxx.xxx.xxx/ws/lexopolygame/webservice.php) is not a WSDL or Webservice url -

but still you can use URLRequestMethod.POST to get the solution..

See the code bellow and the attached fla file with this answer...


Code: Select all

import flash.display.Sprite;
import flash.events.*;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestHeader;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;






btn1.addEventListener(MouseEvent.CLICK, passData)


function passData(e:MouseEvent){





    var request:URLRequest = new URLRequest ("http://xxx.xxx.xxx.xxx/ws/lexopolygame/webservice.php");
    request.method = URLRequestMethod.POST;

    var variables:URLVariables = new URLVariables();

     
   
    variables.method='register'
    variables.uname='test1'
     variables.mail=txt1.text
     variables.password1='password'
     variables.password2='password'
     variables.age='27'
     variables.country='IN'
   
   
   
   
    request.data = variables;

    var loader:URLLoader = new URLLoader (request);

    // configuring listeners...
    loader.addEventListener(Event.COMPLETE, onComplete);
    loader.addEventListener(Event.OPEN, openHandler);
    loader.addEventListener(ProgressEvent.PROGRESS, progressHandler);
    loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
    loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
    loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);

    //
    loader.dataFormat = URLLoaderDataFormat.TEXT;
    loader.load(request);


    function onComplete(event:Event):void {
       trace("Result is "+event.target.data);
      resultTxt.text=event.target.data
    }
    /////////////////////////////////////////////////////////////

    function httpStatusHandler(event:Event):void {
       trace("httpStatusHandler status: " + event+"\n\n");
    }

    function ioErrorHandler(event:IOErrorEvent):void {
       trace("ioErrorHandler: " + event+"\n\n");
    }

    function openHandler(event:Event):void {
       trace("openHandler: " + event+"\n\n");
    }

    function progressHandler(event:ProgressEvent):void {
       trace("progressHandler loaded:" + event.bytesLoaded + " | total: " + event.bytesTotal+"\n\n");
    }

    function securityErrorHandler(event:SecurityErrorEvent):void {
       trace("securityErrorHandler: " + event+"\n\n");
    }
   
   
}


Get more info about it from here viewtopic.php?f=29&t=16

All the best... :)

Bookmark and Share
Attachments
sendAndLoadAS3.0.zip
(557.48 KiB) Downloaded 217 times
Last edited by Ashish on Wed Apr 27, 2011 1:29 pm, edited 2 times in total.
Ashish
 
Posts: 153
Joined: Tue Oct 14, 2008 1:49 pm

Re: Calling Webservices in Flash Using AS 3.0

Postby sandarya.info on Wed Apr 27, 2011 12:47 pm

thanks for the solution this looks like it will work i'll try and reply you the status,

one more thing do i need to upload crossdomain.xml to the webservice hosting..

thanks

Bookmark and Share
sandarya.info
 
Posts: 3
Joined: Tue Apr 26, 2011 8:47 pm

Re: Calling Webservices in Flash Using AS 3.0

Postby Ashish on Wed Apr 27, 2011 12:53 pm

If your PHP page and flash application is hosted on same server then no need to upload crossdomain. If not both on same server then you have to upload 'Crossdomain.xml' on the root where your php page is exist.

At present scenario (if your flash application is not kept on http://xxx.xxx.xxx.xxx/) the cross domain will be hosted like 'http://xxx.xxx.xxx.xxx/crossdomain.xml'

Ashishk

Bookmark and Share
Last edited by Ashish on Wed Apr 27, 2011 2:51 pm, edited 1 time in total.
Ashish
 
Posts: 153
Joined: Tue Oct 14, 2008 1:49 pm

Re: Calling Webservices in Flash Using AS 3.0

Postby sandarya.info on Wed Apr 27, 2011 2:44 pm

yes your code is working great thanks for everything.

i'll send you the link of the application once i finished. :D

Bookmark and Share
sandarya.info
 
Posts: 3
Joined: Tue Apr 26, 2011 8:47 pm


Return to Flash and Asp.net, C#

Who is online

Users browsing this forum: No registered users and 1 guest

cron