• 【设为首页】
  • 【收藏闪客居】
当前位置:主页>FLASH AS 编程>AS高手篇>文章内容
  • 一个最基本的WebService+Flash调用的实例
  • 来源:RIACN 作者:JimLee 2006-07-02 【

[前几天的一则笔记,随手写的,没有注释。
自己的体会:对于Flash+asp.net的开发,用ws比直接使用flash+xml+asp.net要更快捷一些,而且层次分明,可以让UI设计和后台完全隔离。
对于大数据集的处理,倾向于在服务端撰写相关类,序列化之后输出,这样在客户端处理更快捷,而且适应性更强。
最后一个感受:直接使用WebServiceAPI比使用WebServiceConnector组件更加灵活,直接。

]

C#源文件:testws.asmx(http://localhost/dxl_green_web/testws.asmx) 


<%@ WebService Language="c#" Class="dxl_green_web.test" %> 

using System; 
using System.Data; 
using System.IO; 
using System.Web; 
using System.Web.Services; 

namespace dxl_green_web 

[Serializable] 
public class Person 

public Person() 



public Person(string name,string gender) 

this.Name=name; 
this.Gender=gender; 


public string Name=""; 
public string Gender=""; 


[WebService(Namespace="http://www.dxlschool.com/ws/")] 
public class test : System.Web.Services.WebService 

public test() 




[WebMethod] 
public string HelloWorld() 

return "你好"; 

[WebMethod] 
public string NowD(){ 
return DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"); 


[WebMethod]  
public float[] ALtest(int a){ 
float[] tint=new float[a]; 
int i=0; 
while (i<a) 

tint[i]=(float)i; 
++i; 

return tint; 


[WebMethod] 
public string GetPersonTable() 

DataTable table=new DataTable("Person");  
table.Columns.Add("Name"); 
table.Columns.Add("Gender"); 
table.Rows.Add(new string[2]{"Alice","Female"}); 
table.Rows.Add(new string[2]{"Bob","Male"}); 
table.Rows.Add(new string[2]{"Chris","Female"}); 
table.Rows.Add(new string[2]{"Dennis","Male"}); 
table.Rows.Add(new string[2]{"Eric","Male"}); 

DataSet dataset=new DataSet("PersonTable"); 
dataset.Tables.Add(table); 

System.Text.StringBuilder strbuilder=new System.Text.StringBuilder(); 
StringWriter writer=new StringWriter(strbuilder); 
dataset.WriteXml(writer,System.Data.XmlWriteMode.IgnoreSchema); 

return strbuilder.ToString(); 


[WebMethod] 
public Person[] GetPersons() 

Person Alice=new Person("Alice","Female"); 
Person Bob=new Person("Bob","Male"); 
Person Chris=new Person("Chris","Female"); 
Person Dennis=new Person("Dennis","Male"); 

return new Person[]{Alice,Bob,Chris,Dennis}; 





}
 

AS2源文件 
import mx.services.*; 

import mx.services.*; 
myWebSrvc = new WebService("http://localhost/dxl_green_web/testws.asmx?wsdl"); 
hl = myWebSrvc.HelloWorld(); 
hl.onResult = function(result) { 
trace(result); 
}; 
h2 = myWebSrvc.ALtest(100); 
h2.onResult = function(result) { 
for (i=0; i<result.length; ++i) { 
trace(result[i]); 

}; 
h3=myWebSrvc.GetPersons(); 
h3.onResult=function(result){ 
trace(result.length); 
for (i=0; i<result.length; ++i) { 
trace(result[i]["Name"]); 
trace(result[i].Gender); 
}  
}
 


Note:
1、 在多种语言共存的编程环境下,是不适合使用类似DataSet这种只属于特定语言的数据类型的。应该尽量使用简单数据类型以及简单数据类型的数组。
2、 应当很谨慎的决定是否需要通过Web Service来返回大量数据。由于网络传输的开销既包括HTTP连接建立的时间,也包括传送数据的时间,因此需要在减少访问服务器次数和减少网络传输量之间寻找一个合适的平衡。如非必须,则不适合通过Web Service传送含有几十条或者几百条数据的数据表。
3、 使用序列化可以让数据在客户端更容易处理。



上一篇:通过PHP读取Flash文件的头部信息   下一篇:类定义方法中的事件处理范围
  • 用户名:新注册) 密码: 匿名评论
  • 评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规)
推荐内容

Copyright © 2006-2008 flashas.net All Rights Reserved.
网站内容咨询: admin#flashas.net (#为@) 联系QQ:40777822 浙ICP备06033001号
(本网站最佳浏览解析度为1024*768, 建议使用IE 6.0或以上版本浏览器。)