Flex 数据访问 WebService (上)
摘要:WebService组件用于访问SOAP Web服务,此类服务时带有方法的软件模块,Web服务方法通常称为“操作(option)”,操作可以带参数(requet)。Web服务接口通过 Web 服务描述语言 (WSDL) 进行定义。通过 Web 服务提供的标准相容方式,在不同平台上运行的软件模块可以相互交互。
Flex 支持格式设置为 SOAP 消息且通过 HTTP 传输的 Web 服务请求和结果。SOAP 提供基于 XML 格式的定义,用于在 Web 服务客户端(如使用 Flex 构建的应用程序)和 Web 服务之间交换结构化和类型化信息。
在.NET和Flex的数据交互可以通过Web Services访问string,object,datatable,List<>,ArrayList等。
.NET和Flex的数据示例:
1、返回对象
定义返回对象的Web Method:
[WebMethod]
public Employee GetEmployee()
{
return new Employee
{
id = 1,
name = "Shawn",
age = 25
};
}
Flex前端代码:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
private function onResult(event:ResultEvent):void
{
Alert.show(event.result.name);
}
private function onFault(event:FaultEvent):void
{
Alert.show("调+event.message);
}
private function GetEmployee():void
{
this.MyService.GetEmployee.send();
}
]]>
</mx:Script>
<mx:Button label="Get Employee" click="GetEmployee()"/>
<mx:WebService id="MyService" wsdl="http://localhost:4081Flex.asmx?WSDL" useProxy="false" result="onResult(event)" fault="onFault(event)">
<mx:operation name="GetEmployee"/>
</mx:WebService>
</mx:Application>
运行结果:
2、返回DataTable
定义返回DataTable的Web Method:
[WebMethod]
public DataTable GetDataTable()
{
DataTable dt = new DataTable("Employees");
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("age", typeof(int));
DataRow dr = dt.NewRow();
dr["id"] = 1;
dr["name"] = "Shawn";
dr["age"] = 25;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["id"] = 2;
dr["name"] = "Jack";
dr["age"] = 23;
dt.Rows.Add(dr);
return dt;
}
Flex前端代码:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
private function onResult(event:ResultEvent):void
{
}
private function onFault(event:FaultEvent):void
{
Alert.show("调+event.message);
}
private function GetDataTable():void
{
this.MyService.GetDataTable.send();
}
]]>
</mx:Script>
<mx:Panel width="400">
<mx:DataGrid id="gvEmployee" dataProvider="{this.MyService.GetDataTable.lastResult.Tables.Employees.Rows}" width="100%">
<mx:columns>
<mx:DataGridColumn headerText="ID" dataField="id"/>
<mx:DataGridColumn headerText="Name" dataField="name"/>
<mx:DataGridColumn headerText="Age" dataField="age"/>
</mx:columns>
</mx:DataGrid>
<mx:ControlBar>
<mx:Button label="Get DataTable" click="GetDataTable()"/>
</mx:ControlBar>
</mx:Panel>
<mx:WebService id="MyService" wsdl="http://localhost:4081/Flex.asmx?WSDL" useProxy="false" result="onResult(event)" fault="onFault(event)">
<mx:operation name="GetDataTable"/>
</mx:WebService>
</mx:Application>
运行结果:
3、返回List<>
[WebMethod]
public List<Employee> GetEmployeeList()
{
return new List<Employee>
{
new Employee
{
id = 1,
name = "Shawn",
age = 25
},
new Employee
{
id = 2,
name = "Jack",
age = 23
}
};
}
Flex前端:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
private function onResult(event:ResultEvent):void
{
var arr:ArrayCollection = this.MyService.GetEmployeeList.lastResult as ArrayCollection;
gvEmployee.dataProvider=arr;
}
private function onFault(event:FaultEvent):void
{
Alert.show("调+event.message);
}
private function GetEmployeeList():void
{
this.MyService.GetEmployeeList.send();
}
]]>
</mx:Script>
<mx:Panel width="400">
<mx:DataGrid id="gvEmployee" width="100%">
<mx:columns>
<mx:DataGridColumn headerText="ID" dataField="id"/>
<mx:DataGridColumn headerText="Name" dataField="name"/>
<mx:DataGridColumn headerText="Age" dataField="age"/>
</mx:columns>
</mx:DataGrid>
<mx:ControlBar>
<mx:Button label="Get List" click="GetEmployeeList()"/>
</mx:ControlBar>
</mx:Panel>
<mx:WebService id="MyService" wsdl="http://localhost:4081/Flex.asmx?WSDL" useProxy="false" result="onResult(event)" fault="onFault(event)">
<mx:operation name="GetEmployeeList"/>
</mx:WebService>
</mx:Application>