有時你要鏈接的服務並不是是SOAP風格的。其餘經常使用的框架能夠結合使用HTTP Get和Post方法來返回數據。使用<mx:HTTPService>標籤能夠讓你訪問這種類型的WEB服務。
使用HTTP Post和Get方法操做的WEB服務能夠理解爲REST服務。在不少狀況下,使用HTTP Post方法。使用HTTP Web服務的一個明顯的好處是你能夠傳遞比使用SOAPWeb服務時更少的數據。
Adobe Flexx 數據訪問組件使用遠程過程調用(RPC)來與服務器環境交互,例如PHP,Adobe ColdFusion,和Microsoft ASP.NET,來爲Flex程序提供數據,以及將數據傳遞給後臺的數據源。
鏈接到一個HTTP Web服務
能夠很容易的使用<mx:HTTPSevice>。<mx:HTTPService>有着相似<mx:WebService>和<mx:HTTPService>標籤的屬性,且具備falt和result時間供你監聽和處理,以下所示:
<
mx:HTTPService
id
="yahooHTTPService"
url
="http://search.yahooapis.com/WebSearchService/V1/webSearch"
method
="GET"
makeObjectsBindable
="true"
result
="httpServiceResult(event)"
fault
="httpServiceFault(event)"
showBusyCursor
="true"
>
</
mx:HTTPService
>
和使用<mx:WebService>標籤時不一樣,你不須要定義操做。全部要發送到服務器進行處理的變量將做爲鍵值對傳到url屬性定義的URL。這個標籤的行爲與HTML表單的get或post的行爲相同。要向服務器傳遞數據,你只須要傳遞攜帶着那些變量的對象。Flex會將那些變量封裝進正確的格式。下面的代碼展現了對上面<mx:HTTPService>標記進行處理的ActionScript代碼:
public function httpServiceResult(e:ResultEvent):void { Alert.show("received a result"); } public function httpServiceFault(e:FaultEvent):void { Alert.show("received a Fault"); } public function sendHttpRequest():void { var requestObj:Object = new Object(); requestObj.appid = new String("YahooDemo"); requestObj.query = new String("persimmon"); requestObj.results = new int(2); yahooHTTPService. request = requestObj; httpAsyncToken = yahooHTTPService.send(); }