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();
import mx.services.*;
_global.webServicen =
new WebService("http://localhost/WebServiceAndFlash/login.asmx?WSDL");
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");
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";
}
}