至於後端WebService
我是使用Java Apache Axis,以JWS的方式建置
連線的時候,要注意Flash Crossdomain Security
請自行參考相關文件,加上crossdomain.xml
嘗試錯誤的過程中~
覺得Flex 2.0 AS 3.0的設計實在有些奇怪
像是webservice.getOperation回傳型別
以及< mx : operation ..... >為什麼是小寫
還有WebService的Operation,都只能夠用getOperation取得
可是文件上明明就有從Operation建構子傳入ws的方式
但是就是無法成功執行動作~~
當然目前最大的問題就是相關文件相當缺乏
我這樣寫法也不知道是不是標準的做法
Flex 2.0 WebService Tag MXML:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2005/mxml" xmlns="*"
creationComplete="init();">
<mx:Script>
<![CDATA[
import mx.rpc.*;
import mx.rpc.soap.*;
import mx.rpc.events.*;
import flash.util.*;
public function init():Void {
/*/
trace("ws.endpointURI : " + ws.endpointURI);
trace("ws.wsdl : " + ws.wsdl);
trace("ws.port : " + ws.port);
trace("ws.rootURL : " + ws.rootURL);
trace("ws.service : " + ws.service);
trace("ws.useProxy : " + ws.useProxy);
trace(ws.hasEventListener("load"));
//*/
}
public function fault(target:AbstractOperation):Void {
trace("fault() " + target);
output.text += result + "\n";
}
public function result(target:AbstractOperation):Void {
trace("result() " + target.result);
output.text += target.result + "\n";
for(var i:String in target.result) {
trace("result[" + i + "] : " + target.result[i]);
output.text += "result[" + i + "] : " + target.result[i] + "\n";
}
}
public function load():Void {
trace("wsdl loaded");
output.text += "wsdl loaded\n";
}
]]>
</mx:Script>
<mx:WebService id="ws"
load="load()"
useProxy="false"
wsdl="http://localhost:8080/axis/Test.jws?wsdl">
<mx:operation name="getDate" fault="fault(ws.getOperation('getDate'))"
result="result(ws.getOperation('getDate'))" />
<mx:operation name="getObj" fault="fault(ws.getOperation('getObj'))"
result="result(ws.getOperation('getObj'))" />
</mx:WebService>
<mx:Canvas width="100%" height="100%">
<mx:TextArea x="45" y="73" width="321" height="269" id="output"/>
<mx:Button label="Get Date"
click="ws.getOperation('getDate').send();" x="92" y="34" />
<mx:Button label="Get Obj"
click="ws.getOperation('getObj').send({aa:1, bb:2});" x="172" y="34" />
</mx:Canvas>
</mx:Application>
Flex 2.0 WebService AS 3.0 MXML:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2005/mxml" xmlns="*"
creationComplete="init();">
<mx:Script>
<![CDATA[
import mx.rpc.*;
import mx.rpc.soap.*;
import mx.rpc.events.*;
import flash.util.*;
var wsdl:String = "http://localhost:8080/axis/Test.jws?wsdl";
var ws:WebService;
var getDate:AbstractOperation;
var getObj:AbstractOperation;
public function init():Void {
ws = new WebService();
ws.wsdl = wsdl;
var load:Function = function (evt:Event) {
trace("wsdl loaded");
output.text += "wsdl loaded";
}
ws.addEventListener("load", load);
ws.fetchWSDL();
getDate = ws.getOperation("getDate");
getObj = ws.getOperation("getObj");
/*/
trace(getDate);
trace("operation is Operation : " + (getDate is Operation));
trace("operation.resultFormat : " + Operation(getDate).resultFormat);
trace("operation.targetNamespace : " + Operation(getDate).targetNamespace);
trace("operation.endpointURI : " + Operation(getDate).endpointURI);
//*/
getDate.addEventListener("fault", fault);
getDate.addEventListener("result", result);
getObj.addEventListener("fault", fault);
getObj.addEventListener("result", result);
}
public function result(evt:ResultEvent):Void {
trace("result : " + evt.result);
trace("target : " + evt.target);
output.text += evt.result + "\n";
for(var i:String in evt.result) {
output.text += "result[" + i + "] : " + evt.result[i] + "\n";
trace("result[" + i + "] : " + evt.result[i] + "\n");
}
}
public function fault(evt:FaultEvent):Void {
trace("fault");
output.text += "fault\n";
}
]]>
</mx:Script>
<mx:Canvas width="100%" height="100%">
<mx:TextArea x="45" y="73" width="321" height="269" id="output"/>
<mx:Button label="Get Date" click="getDate.send();" x="78" y="32" />
<mx:Button label="Get Obj" click="getObj.send({a:1, b:2});" x="154" y="32" />
</mx:Canvas>
</mx:Application>
Axis JWS:
import java.util.*;
public class Test {
public String getDate () {
return new Date().toString();
}
public HashMap getObj (HashMap map) {
Object[] key = map.keySet().toArray();
for(int i = 0 ; i < key.length ; ++i) {
System.out.println("map[" + key[i] + "] : " + map.get(key[i]));
}
return map;
}
}