Click here to Skip to main content
Click here to Skip to main content

Flash and Web Services

By , 4 Jan 2006
 

Introduction

This sample will log you in with a Web Service, serialize the data from a database, and use the serialization class in Flash.

Background

You can get data from a server application in several ways, but I discovered that when working with .NET, using a serialization class is very easy.

Using the code

We will use the a Web Service and a serilazation class and a function in Flash to get the user information. Here is the login method in the Web Service:

[WebMethod]
public oUsers loginUser(string sUsername, string sPassword)
{
    oUsers objUsers = new oUsers();
    try
    {
        if( sUsername.Equals("admin") && 
            sPassword.Equals("admin") )
        {
            objUsers.sEmail = "admin@administrator.com";
            objUsers.sName = "Adam";
            objUsers.sPassword = "admin";
            objUsers.sUsername = "admin";
        }
        else
        {
            throw new ApplicationException("Wrong user!");
        }
    }
    catch
    {
        throw new ApplicationException("Error");
    }
    return objUsers;
}

Here is the serialization class:

[Serializable]
public class oUsers
{
    public string sName;
    public string sEmail;
    public string sUsername;
    public string sPassword;

    public oUsers()
    {
    }
}

Here is the Flash script for the root:

//stop the secvens of the time line
stop();

//importing the webservice classes
import mx.services.*;

//declareing the an global variable for easy access
_global.webServicen = 
  new WebService("http://localhost/WebServiceAndFlash/login.asmx?WSDL");

//cleaning up the textboxes

mcLogin.txtUsername.text = "admin";
mcLogin.txtPassword.text = "admin";
mcLogin.txtName.text = "";
mcLogin.txtEmail.text = "";
mcLogin.txtUser.text = "";
mcLogin.txtPass.text = "";

Given below is the script for the OnClick function:

on(release){
    var objServicen = _global.webServicen.loginUser(
        _root.mcLogin.txtUsername, _root.mcLogin.txtPassword);
    objServicen.onResult = function(result)
    {
        trace("login");
        //declare the Userobject

        var oUser = new objServicen.oUsers();
        oUser = result;
        
        _root.mcLogin.txtName = "SDF" + oUser.sName;
        _root.mcLogin.txtEmail = oUser.sEmail;
        _root.mcLogin.txtUser = oUser.sUsername;
        _root.mcLogin.txtPass = oUser.sPassword;
        
    }
    
    objServicen.onFault = function(fault)
    {
        _root.mcAlert._x = "-0.3";
        _root.mcAlert._y = "-0.7";
    }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

daniel_nilsson
Architect Värderingsdata
Sweden Sweden
Systemdeveloper at Värderingsdata in Sweden

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
General2010 and yoour code helped mememberZED023-Jun-10 3:19 
Thank you so much your code gave me a push in the right direction. It works perfectly.
Generalcommunication problem on http environnementmemberquineman25-Mar-08 1:46 
Hi, Actualy I have got a problem when executing my method in browser.
the log say me :
Faulting previously queued call "myMethod"
anybody have this problem ?
QuestionHow to pass a custom object to the WebService ?memberYasin7523-May-07 23:15 
Hi,
 
I have read the articles (part 1 and 2).
Is it possible to pass a custom object from flash to the web service?
 
i.e:
 
public myCustomEmpleeCollection   MyWebMethod(MyCustomEmpleeSearch myCustomEmpleeSearchObject )
{
   // do something with myCustomEmpleeSearchObject
   // and return a custom collection
 
   return new myCustomEmpleeCollection ();
}
 

How do I call such a method from flash?   keeping in mind that the objects' class defition is in the Server side C# code.
 
Regards,
   Yasin.
 


 
Yasin
QuestionFault Handler with Error Information?membergollum915-Feb-07 3:13 
Hello Daniel,
 
im wondering if your Application is handling Exceptions properly.
 
In your Webservice you are throwing different "kinds" of Exceptions respectively different Error-Messages:

...
throw new ApplicationException("Wrong user!");
...
throw new ApplicationException("Error");
...

 
In my opinion its not possible to get these qualified Error-Messages in an Flash Application because its returned with an HTTP Header Status Code 500.
 
What are your experiences?
 
Thanks,
BR
Nils
QuestionSequrity (global sequrity configuration)membersympthom 99-Feb-07 2:53 
Hi,
 
I can not get the WebService request to work in a webpage.
 
If I execute the swf file alone it works. Then I get a security information promted, that the swf file is trying to access the webservice. After I configurate this It all work fine.
 
How do you configure the global sequrity configuration for a swf file hosted within a page?
 
Cheers,
Martin
GeneralCan't open the csproj in VS2003memberLotharLanger5-Jan-06 3:05 
Hi Daniel,
 
sorry i am completely new in the business of web services.
So I tried to open your project from Visual Studio 2003 and it doesn't open. Instead i get a message saying that there is no web server detected at URL 'http://localhost:/WebServiceAndFlash'.
What do i have to do before opening the csproj?
 
Cheers,
Lothar
GeneralRe: Can't open the csproj in VS2003memberdaniel_nilsson5-Jan-06 7:42 
ok
 
you can open the "WebServiceAndFlash.csproj.webinfo" file in notepad and chek if the UrlPath is correct
 
the path should be as your IIS is configured
 
good luck
 

please send mail if it dosent work
GeneralRe: Can't open the csproj in VS2003memberLotharLanger5-Jan-06 9:34 
I had to install IIS on my machine. Now I am getting another message when trying to open the csproj: the specified web server is not running ASP.NET version 1.1. You will be unable to run ASP.NET web applications and services.
 
Is it that I have to get a newer version of IIS from the MS download page ? I am sure i recently installed the .NET Frameword 2.0 already.
 
Thanks for your help,
Lothar
GeneralRe: Can't open the csproj in VS2003memberdaniel_nilsson7-Jan-06 23:11 
ok this test is for 1.1 but i think you can convert it to 2.0
 
if you google on the errors you get you get the solutions
QuestionDoes anyone have pointers to further reading?memberMindphaser4-Jan-06 11:08 
online articles, books, anything is of interest.
AnswerRe: Does anyone have pointers to further reading?memberdaniel_nilsson4-Jan-06 21:31 
What are you intrested in Flash or the server technics??
 
Here is some links and books
http://www.flash-remoting.com/
http://www.macromedia.com/devnet/flash/webservices.html
http://www.oreillynet.com/pub/a/javascript/2003/01/09/flash.html
 
//Daniel Nilsson
 
-- modified at 3:34 Thursday 5th January, 2006
GeneralRe: Does anyone have pointers to further reading?memberMindphaser4-Jan-06 22:36 
I'm especially interested in what can be done with nothing but PHP5 and MySQL installed server-side.
GeneralRe: Does anyone have pointers to further reading?memberdaniel_nilsson7-Jan-06 23:12 
you can google on php flash remoting

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130617.1 | Last Updated 4 Jan 2006
Article Copyright 2006 by daniel_nilsson
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid