惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
Martin Fowler
Martin Fowler
Last Week in AI
Last Week in AI
罗磊的独立博客
阮一峰的网络日志
阮一峰的网络日志
博客园 - 【当耐特】
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
V
Visual Studio Blog
Hugging Face - Blog
Hugging Face - Blog
雷峰网
雷峰网
博客园_首页
人人都是产品经理
人人都是产品经理
量子位
美团技术团队
The Cloudflare Blog
小众软件
小众软件
WordPress大学
WordPress大学
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
Microsoft Security Blog
Microsoft Security Blog
D
DataBreaches.Net
博客园 - Franky

博客园 - spoony

Flex4 Skinning 4: 留言板示例 Flex4 Skinning 2: 皮肤协议 Flex4 Skinning 1: 自定义一个简单按钮的皮肤 Air应用程序访问本地数据库 [通篇转载]调程序的小女孩 Adobe ColdFusion Reports(5) Adobe ColdFusion Reports(4) Adobe ColdFusion Reports(3) Adobe ColdFusion Reports(2) Adobe ColdFusion Reports(1) Adobe ColdFusion Charts(3) Adobe ColdFusion Charts(2) Adobe ColdFusion Charts(1) Adobe ColdFusion读写Microsoft Office Excel数据(3) Adobe ColdFusion读写Microsoft Office Excel数据(2) Adobe ColdFusion读写Microsoft Office Excel数据(1) Flex与DotNET WebService交互(2) Ext JS Designer 1.0.5 发布 设置Flex Builder中FlashPlayer调试版本 - spoony - 博客园
Flex与DotNET WebService交互(1)
spoony · 2009-12-26 · via 博客园 - spoony

  Flex常用的访问远程数据的类有:HTTPService, WebService, RemoteObject. HttPService WebService自不必说,使用的是基于XMLSOAP(Simple Object Access Protocol, 简单对象访问协议),而RemoteObject使用的却是序列化的二进制协议AMF. AMF(Action Message Format)是在flashflexremoting的一种方式格式。笔者在这里并不太想讨论这个有点“非主流”的方式,只讨论一下FlexDotNET WebService的交互。

调用DotNETWebService会自动执行序列化的操作。

  序列化是将一个对象保存到存储介质上或者将对象进行转换使之能够在网络上传送的行为。在一个对象被序列化之后,如果想使用,需要将它反序列化,也就是将数据重新转换为可用的对象的行为。序列化主要用途有两种,一是用在一个对象必须被从一个上下文封送到另一个上下文的时候,例如当对象跨越App域的时候。另外一个例子是Web服务——对象在服务器上被序列化,通过网络被(封送或)发送到的客户端,然后被反序列化成有用的对象。

  利用FlexDotNET WebService交互,用到的正是上文提到的序列化的第二个用途。序列化得到的XML格式数据,可以通过Flex中的WebService标签进行获取并利用DataGrid组件将数据显示出来。

  首先来看一个最简单的实例:

  先创建最基本的类:

	<mx:Script>
		<![CDATA[

			internal var employees:ArrayCollection;
			internal function onList(event:ResultEvent):void
			{
				employees = new ArrayCollection();
				employees = event.result as ArrayCollection;
				this.dgEmployees.dataProvider = employees;
			}
		]]>

	<mx:WebService id="webService"
		wsdl="http://localhost:2492/Service1.asmx?wsdl">
		<mx:operation name="listEmployees" 
			result="onList(event)" 
			fault="onFault(event)" />
	</mx:WebService>
			<mx:DataGrid id="dgEmployees">
				<mx:columns>
					<mx:DataGridColumn headerText="ID" dataField="Id"/>
					<mx:DataGridColumn headerText="NAME" dataField="Name"/>
				</mx:columns>
			</mx:DataGrid>
			<mx:Button id="btnList" label="List" click="webService.listEmployees();" />

   然后创建WebService中的WebMethod

        /// <summary>
        /// webmethod used to return an object collection
        /// </summary>
        /// <returns>employees</returns>
        [WebMethod]
        public List<Employee> listEmployees()
        {
            List<Employee> employees = new List<Employee>();

            employees.Add(new Employee("006", "Aking"));
            // To test the one-record-returned result, comment the following line. 
            employees.Add(new Employee("007", "spoony"));
            
            return employees;
        }

  心急一下,先看看浏览WebService的效果:

  注意此处的协议为HTTP POST,

产生的内容如下:

<?xml version="1.0" encoding="utf-8" ?> 
<ArrayOfEmployee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<Employee>
  		<Id>006</Id> 
  		<Name>Aking</Name> 
  </Employee>
<Employee>
  		<Id>007</Id> 
  		<Name>spoony</Name> 
</Employee>
</ArrayOfEmployee>

  Flex应用程序的相关代码如下:

    /// <summary>
    /// Business entity used to model an emplayee. 
    /// </summary>
    public class Employee
    {
        /// <summary>
        /// identity 
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// name
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// constructors
        /// </summary>
        public Employee() { }
        public Employee(string id, string name)
        {
            this.Id = id;
            this.Name = name;
        }
    }

   最后来看看运行效果:

这样一个最简单的FlexDotNET WebService交互示例就完成了。