Flex 數據訪問 WebService 使用參數(下)
上一篇介紹Flex的WebService的使用,可以調用多種類型的數據,都是直接調用,沒有使用參數,本篇學習使用參數調用WebService,WebService的參數類型可以是:簡單類型(如數值,字串串等),簡單實體模型(只有屬性),比較復雜的實體模型(內陷其他實體),以及集合,XML等。
Flex在調用不同后臺實現的Web Method方式只是在構造參數有些區別,調用方式是一樣的,以下簡單介紹Flex調用.NET的Web Method使用不同參數。
定義Web Method用到的類:
[Serializable]
public class Employee
{
public int id { get; set; }
public string name { get; set; }
public int age { get; set; }
}
[Serializable]
public class Dept
{
public int DeptID{ get; set; }
public string DeptName { get; set; }
public Employee[] Employees { get; set; }
}
public class Employee
{
public int id { get; set; }
public string name { get; set; }
public int age { get; set; }
}
[Serializable]
public class Dept
{
public int DeptID{ get; set; }
public string DeptName { get; set; }
public Employee[] Employees { get; set; }
}
一、簡單類型參數
Web Method定義:
[WebMethod]
public Employee GetEmployee(int id)
{
return new Employee
{
id = id,
name = "Employee"+id,
age = 25
};
}
public Employee GetEmployee(int id)
{
return new Employee
{
id = id,
name = "Employee"+id,
age = 25
};
}
Flex可以直接定義operation的時候定義requests:
<?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.soap.mxml.Operation;
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 msg:String="ID:"+event.result.id+"\n"+
"Name:"+event.result.name+"\n"+
"Age:"+event.result.age;
Alert.show(msg);
}
private function onFault(event:FaultEvent):void
{
Alert.show("Error:"+event.message);
}
private function GetEmployee():void
{
MyService.GetEmployee.send();
}
]]>
</mx:Script>
<mx:Button label="GetEmployee" click="GetEmployee()"/>
<mx:WebService id="MyService" wsdl="http://localhost:4081/Flex.asmx?WSDL" useProxy="false"
result="onResult(event)" fault="onFault(event)">
<mx:operation name="GetEmployee">
<mx:request xmlns="">
<id>1</id>
</mx:request>
</mx:operation>
</mx:WebService>
</mx:Application>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.rpc.soap.mxml.Operation;
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 msg:String="ID:"+event.result.id+"\n"+
"Name:"+event.result.name+"\n"+
"Age:"+event.result.age;
Alert.show(msg);
}
private function onFault(event:FaultEvent):void
{
Alert.show("Error:"+event.message);
}
private function GetEmployee():void
{
MyService.GetEmployee.send();
}
]]>
</mx:Script>
<mx:Button label="GetEmployee" click="GetEmployee()"/>
<mx:WebService id="MyService" wsdl="http://localhost:4081/Flex.asmx?WSDL" useProxy="false"
result="onResult(event)" fault="onFault(event)">
<mx:operation name="GetEmployee">
<mx:request xmlns="">
<id>1</id>
</mx:request>
</mx:operation>
</mx:WebService>
</mx:Application>
運行結果:
二、簡單對象
Web Method定義,模擬添加對象,Flex前端代碼,參數名跟Web Method的參數名一樣。
[WebMethod]
public int AddEmployee(Employee employee)
{
return 1;
}
public int AddEmployee(Employee employee)
{
return 1;
}
運行結果:
跟蹤Web Method的employee參數:
三、對象數組
Web Method定義,添加多個對象:
[WebMethod]
public int AddEmployees(Employee[] list)
{
return list.Length;
}
public int AddEmployees(Employee[] list)
{
return list.Length;
}
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.soap.mxml.Operation;
import mx.collections.ArrayCollection;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
private function onResult(event:ResultEvent):void
{
Alert.show(event.result.toString());
}
private function onFault(event:FaultEvent):void
{
Alert.show("Error:"+event.message);
}
private function AddEmployee():void
{
var empArr:Array=new Array();
empArr.push({id:0,name:"user1",age:22});
empArr.push({id:0,name:"user2",age:23});
empArr.push({id:0,name:"user3",age:25});
MyService.AddEmployees(empArr);
}
]]>
</mx:Script>
<mx:Button label="AddEmployee" click="AddEmployee()"/>
<mx:WebService id="MyService" wsdl="http://localhost:4081/Flex.asmx?WSDL" useProxy="false"
result="onResult(event)" fault="onFault(event)">
<mx:operation name="AddEmployees">
</mx:operation>
</mx:WebService>
</mx:Application>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.rpc.soap.mxml.Operation;
import mx.collections.ArrayCollection;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
private function onResult(event:ResultEvent):void
{
Alert.show(event.result.toString());
}
private function onFault(event:FaultEvent):void
{
Alert.show("Error:"+event.message);
}
private function AddEmployee():void
{
var empArr:Array=new Array();
empArr.push({id:0,name:"user1",age:22});
empArr.push({id:0,name:"user2",age:23});
empArr.push({id:0,name:"user3",age:25});
MyService.AddEmployees(empArr);
}
]]>
</mx:Script>
<mx:Button label="AddEmployee" click="AddEmployee()"/>
<mx:WebService id="MyService" wsdl="http://localhost:4081/Flex.asmx?WSDL" useProxy="false"
result="onResult(event)" fault="onFault(event)">
<mx:operation name="AddEmployees">
</mx:operation>
</mx:WebService>
</mx:Application>
四、復雜對象
Web Method定義:
[WebMethod]
public int AddDept(Dept dept)
{
return 1;
}
public int AddDept(Dept dept)
{
return 1;
}
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.soap.mxml.Operation;
import mx.collections.ArrayCollection;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
private function onResult(event:ResultEvent):void
{
Alert.show(event.result.toString());
}
private function onFault(event:FaultEvent):void
{
Alert.show("Error:+event.message);
}
private function AddDept():void
{
var empArr:Array=new Array();
empArr.push({id:0,name:"user1",age:22});
empArr.push({id:0,name:"user2",age:23});
empArr.push({id:0,name:"user3",age:25});
var dept:Object=new Object();
dept.DeptID=1;
dept.DeptName="dept1";
dept.Employees=empArr;
MyService.AddDept(dept);
}
]]>
</mx:Script>
<mx:Button label="AddDept" click="AddDept()"/>
<mx:WebService id="MyService" wsdl="http://localhost:4081/Flex.asmx?WSDL" useProxy="false"
result="onResult(event)" fault="onFault(event)">
<mx:operation name="AddDept">
</mx:operation>
</mx:WebService>
</mx:Application>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.rpc.soap.mxml.Operation;
import mx.collections.ArrayCollection;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
private function onResult(event:ResultEvent):void
{
Alert.show(event.result.toString());
}
private function onFault(event:FaultEvent):void
{
Alert.show("Error:+event.message);
}
private function AddDept():void
{
var empArr:Array=new Array();
empArr.push({id:0,name:"user1",age:22});
empArr.push({id:0,name:"user2",age:23});
empArr.push({id:0,name:"user3",age:25});
var dept:Object=new Object();
dept.DeptID=1;
dept.DeptName="dept1";
dept.Employees=empArr;
MyService.AddDept(dept);
}
]]>
</mx:Script>
<mx:Button label="AddDept" click="AddDept()"/>
<mx:WebService id="MyService" wsdl="http://localhost:4081/Flex.asmx?WSDL" useProxy="false"
result="onResult(event)" fault="onFault(event)">
<mx:operation name="AddDept">
</mx:operation>
</mx:WebService>
</mx:Application>
運行結果:
全站熱搜