Entry

flash actionscript web services how to

ok well i finally had to revisit web services. it’s been awhile i admit. i tested them out when they first came out and just said (why do i need you - don’t laugh). so now i’m back at them for a client.
i’ve found that flash comes with documentation, but a google only finds results [...]

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • StumbleUpon
  • Google
  • Technorati
  • Slashdot
  • LinkedIn
  • E-mail this story to a friend!

ok well i finally had to revisit web services. it’s been awhile i admit. i tested them out when they first came out and just said (why do i need you - don’t laugh). so now i’m back at them for a client.

i’ve found that flash comes with documentation, but a google only finds results that teach you to either use the web service component or loadVars (wtf?).

so here is my not so tutorial tutorial - lol.

p.s. if i’m missing something somebody tell me

ok theres three methods to do a web service. i am using flash 8 professional. i’m going to cover the web service class, the web service connector class, and controlling a web service competent via actionscript.

Web Service Component

ok i’m doing a down and dirty run though on the component ( trust me google has enough in depth ones) . again this non tutorial tutorial covering using web services with actionscript. not a tutorial on using web service components, or any other components

first go to components >> data >> web service connector

Select new web service

drag it on the stage, select it, go to parameters.

ok you should now see the following
New Component

now in WSDLURL put the url of the WSDL for your web service, name your component. i’ll say you named your component “service”

now for the code :

to setup the parameters for your web service call : service.params=[value1,vaule2,....]

now lets tell the component what function to call at our web service:
service.operation=”function name”

and last but not least lets send the request:
service.trigger()

i’m not gonna get into listeners, etc. but i will tell you were to get the response or results from the server:
service.results - this variable is created once the request completes, it will contain the data returned from the web service call. one thing i think i should note is that if your web service returns xml data, i’ve found any web service methods that use either the connector class or component parse the xml into elements. don’t worry i’ll show you how you access a web service and get an xml response shortly.

Web Service Connector

Yippie now we’re at pure code. i’m providing code directly from from flash help, and will then break it down. you will need to import the web service connector class by adding the following line to the top of your as

import mx.data.components.WebServiceConnector;

the code :

  1. var wsConn:WebServiceConnector = new WebServiceConnector();
  2. wsConn.WSDLURL = “http://www.flash-mx.com/mm/tips/tips.cfc?wsdl”;
  3. wsConn.operation = “getTipByProduct”;
  4. wsConn.params = ["Flash"];
  5. wsConn.suppressInvalidCalls = true;
  6. wsConn.multipleSimultaneousAllowed = false;
  7. wsConn.trigger();

Ok at line 1 we setup a new connector - seems pretty straight forward.

Next at line 2 we tell our new connector where to access the WSDL for our service. this should look familiar as we had to do the same thing for the component we used in the last example. thats because they are basicly the same thing, just one is a component, and one is pure as.

at line 3 we provide the operation, again same as the component.

line 4 we pass the parameters - we covered this last example

line 5 is where we tell the connector if it should make sure we only send valid calls. this allows us to ensure that we only send valid calls to our service.

line 6 tells our connector if we should allow more than one connection at the same time.

line 7 trigger() - we’ve already covered this.

the web service connector component and class use the same functions, have the same variables, etc. so you can use all of the functions, properties, etc - except for the class definition of course.

Web Service Class

Last but by no means least we reach my favorite method : the web service class.

The web service class i like because unlike the web service connector component and class, it is more object oriented in my opinion.

heres some code - straight out of a current project, of course i had to remove the actual service url.

/*
this is an example of real world use
this function is used to send data from the flash file, to a sms / purchase webservice
*/

function sendServiceData() {

//my file has functions that return the id’s of the phone carrier selected, and the channel selected
var carrierID = carriers[GetArrayIndex(carriers, carrier)].id;
var channelID = channels[GetArrayIndex(channels, channel)].id;

//some debugging stuff
carrierIDsText.text = carrierID;
phoneNumberText.text = numberEntry.enterNumber.text;
channelIDsText.text = channelID;

//set up an error message to send to the user if the request fails
errorMess=”Please check your number and try again”

//setup our new service
smsService=new WebService(”http://smsServiceURL”);

//if the service fails to load the WSDL
smsService.onFault=function(result){
if(result){
serviceStatus=false
numberEntry.errorText=errorMess
}
}
//if the service loads the WSDL ok
smsService.onLoad=function(result){
if(result) serviceStatus=true
}

//send a new sms service request
responded =smsService.sendSMS(carrierID,numberEntry.enterNumber.text,channelID);
//if the sms service request loads ok
responded.onResult=function(){
if(serviceStatus){
//place the function to be called if no errors here

}
}

//if the smsm service request fails
responded.onFault=function(){
serviceStatus=false
numberEntry.errorText=errorMess
}
}

The code above isn’t for you to use, but to give you an idea of usage. now lets go through this class and make it all make since.

in order to use the web service class you need to import it by adding the following line.
import mx.services.WebService

now to setup a new web service you do something like this :
myService= new WebService(”web service WSDL URL”)

to make a call you use the name of the function you wish you use, along with any parameters you need to pass:
myServiceCall=myService.ServiceFunction(param1,param2,..)

this creates a pending call object at myServiceCall. hold on relax, in simple terms a pending call object is basicly a listener for the service call you just made. which allows us to listen for the call to complete by using :
myServiceCall.onResult=function(result){whatever you want to do after it completes}
you can also use myServiceCall.onFault in the same way. after the request is completed myServiceCall will contain whatever was returned (also known as the service response).

I’d say we were done, but i know your probaly like : wheres the fault, load etc for the web service class?

the web service, myService in this example, also has a two methods i should mention: onLoad, and onFault. both of these methods refer to the loading of the WSDL for your web service and are used just like you’d used the xml.onLoad method, or the onFault method just covered.

remember how i mentioned the web connector class likes to parse xml data? this class doesn’t. it gives you the entire response wheter it’s a null value, xml, string, etc.

I hope this helps, if so leave a comment so i know to continue documenting the random things i think might help people -

Seacrest - out

Sid

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • StumbleUpon
  • Google
  • Technorati
  • Slashdot
  • LinkedIn
  • E-mail this story to a friend!

One Comment

  1. Omar Sh
    August 7, 2008 at 3:04 pm | Permalink

    dear thanx …
    but i wanna ask u about … the result …
    how can i call the result to put it in text field …
    and the result type is string

    is it like ..

    FinalResult = MyWS_wsc.MyString();
    MyText_txt.text = FinalResult

    or
    MyText_txt.text = MyWS_wsc.target.results
    (i see this in sm example - it was called the opration with prameter and put it in trace() )

    any way … it dosen’t work with me …

    thanx in advanced

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*