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
};
}
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>
<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;
}
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>
<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
}
};
}
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>
<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>
全站熱搜