Flash 和 Web 服务






4.58/5 (10投票s)
2006年1月4日

72786

762
一种使用 Flash 和 Web 服务的一种简单方法。
引言
这个示例将使用 Web 服务登录,序列化数据库中的数据,并在 Flash 中使用序列化类。
背景
你可以通过多种方式从服务器应用程序获取数据,但我发现在使用 .NET 时,使用序列化类非常简单。
使用代码
我们将使用一个 Web 服务、一个序列化类和一个 Flash 函数来获取用户信息。以下是 Web 服务中的登录方法
[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;
}
以下是序列化类
[Serializable]
public class oUsers
{
public string sName;
public string sEmail;
public string sUsername;
public string sPassword;
public oUsers()
{
}
}
以下是 Flash 脚本的根
//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("https:///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 = "";
以下是 OnClick
函数的脚本
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";
}
}