×

Flash ActionScript + WebService 学习第一天

Kalet Kalet 发表于2009-08-11 09:00:55 浏览190 评论0

抢沙发发表评论

在Flash中调用WebService提供的调用数据库内容的方法

因为熟悉的WS的构造方式是基于Asp.net下的,下面所说的都是基于Asp.net的。

Flash作为UI层,有下列好处:
1、 不依赖于具体的平台
2、 轻量级,可以轻易的构造富客户端程序。
3、 可以给用户更好的体验。

但是,Flash在构建复杂的应用时,无法直接与数据库进行交互,必须依赖于其他的中间层进行,比如FlashRemtoing、PHP等等。各种方式都有优缺点。
在Flash与Asp.net进行交互时,最好的方式就是使用WebService。

第一个例子,假设数据库中有一张"UserInfo"表,其中有三个字段"id","username","password"
从WebService开始

先创建一个UserInfo的类
    public class UserInfo
    
{
        
public string id;
        
public string UserName;
        
public string PassWord;
    }
然后在WebService中写一个名为"GetUserList"的方法
        [WebMethod(Description="返回数据集")]
        
public UserInfo[] GetUserList()
        
{
            
public SqlConnection conn = new SqlConnection("workstation id=KCHEN;packet size=4096;user id=sa;data source=KCHEN;persist security info=True;initial catalog=First;password=gef8658");
            UserInfo[] UserList 
= new UserInfo[10];
            SqlCommand SQLcmd 
= new SqlCommand("select TOP 10 * from UserInfo",conn);
            
try
            
{
                conn.Open();
                SqlDataReader reader 
= SQLcmd.ExecuteReader();
                
int i=0;
                
while(reader.Read())
                
{
                    UserInfo UserInfo1 
= new UserInfo();
                    UserInfo1.id 
= reader["id"].ToString();
                    UserInfo1.UserName 
= reader["username"].ToString();
                    UserInfo1.PassWord 
= reader["password"].ToString();
                    UserList[i] 
= UserName1;
                    i
++;
                }

                
return UserList;
            }

            
catch(SqlException e)
            
{
                
string uu = e.Message.ToString();
                
return UserList;
            }

            
finally
            
{
                conn.Close();
            }

        }

上面返回了"UserInfo"类型的数组,在webservice中查看XML的结果为
  <?xml version="1.0" encoding="utf-8" ?> 
 
<ArrayOfUserInfo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/">
 
<UserInfo>
  
<id>1</id> 
  
<UserName>kchen</UserName> 
  
<PassWord>gef8658</PassWord> 
  
</UserInfo>
 
<UserInfo>
  
<id>2</id> 
  
<UserName>kchen30</UserName> 
  
<PassWord>gef8658</PassWord> 
  
</UserInfo>
 
<UserInfo>
  
<id>3</id> 
  
<UserName>kchen40</UserName> 
  
<PassWord>gef8658</PassWord> 
  
</UserInfo>
 
<UserInfo>
  
<id>4</id> 
  
<UserName>我啊</UserName> 
  
<PassWord>dkfd</PassWord> 
  
</UserInfo>
 
<UserInfo>
  
<id>5</id> 
  
<UserName>哇哈哈</UserName> 
  
<PassWord>sfs</PassWord> 
  
</UserInfo>
 
<UserInfo>
  
<id>6</id> 
  
<UserName>郁闷喔</UserName> 
  
<PassWord>dsfsd</PassWord> 
  
</UserInfo>
 
<UserInfo>
  
<id>7</id> 
  
<UserName>sdfsd</UserName> 
  
<PassWord>sdfsfds</PassWord> 
  
</UserInfo>
 
<UserInfo>
  
<id>8</id> 
  
<UserName>fds</UserName> 
  
<PassWord>sdf</PassWord> 
  
</UserInfo>
 
<UserInfo>
  
<id>9</id> 
  
<UserName>dfsdf</UserName> 
  
<PassWord>fs</PassWord> 
  
</UserInfo>
 
<UserInfo>
  
<id>10</id> 
  
<UserName>fs</UserName> 
  
<PassWord>s</PassWord> 
  
</UserInfo>
  
</ArrayOfUserInfo>

此时,在WebService中的代码已经完成,下来是在Flash中
新建一个flash文档,选择菜单中的“窗口”,“开发面板”-“WEB服务”,然后点“定义WEB服务”,在输入框中输入WebService的访问地址,如“http://localhost/first/flashservice.asmx?WSDL”,然后确定,展开刚刚添加的WEBSERIVCE,下面有一个名为"GetUserList()"的方法,右建点击,选择添加方法调用,然后在舞台上拖入一个DATAGRID组件,命名为UserListDB,再拖入一个button组件命名为GetList_btn,在第一桢里写上以下脚本
import mx.services.*
var myws:WebService
=new WebService("http://localhost/First/FlashService.asmx?wsdl"); 
function GetList(result)

//定义一个数组接受ws传值 
var myDB:Array=result; 
//将数组myDB与DataGrid组件gb_main绑定 
UserListDB.dataProvider=myDB; 
}


this.GetList_btn.onPress=function()
op
=myws.GetUserList(); 
op.onResult
=function(result) 
GetList(result); 
}

}
 
然后CTRL+回车测试一下。。

群贤毕至

访客