Flex提供RPC服務來向客戶端提供伺服器端數據。Flex對伺服器端數據提供了相當數量的控制。
使用Flex-RPC服務,我們可以定義要在伺服器端執行的用戶操作。
Flex-RPC服務可以與任何伺服器端技術集成。
Flex-RPC服務之一提供了對壓縮二進位數據的內置支持,可以通過有線傳輸,而且速度非常快。
Flex提供了以下三種類型的RPC服務
Sr.No | RPC Service & Description |
---|---|
1 | http服務 <mx:HTTPService>標記用於表示MXML文件中的HTTPService對象。當您調用HTTPService對象的send()方法時,它會向指定的URL發出一個HTTP請求,並返回一個HTTP響應。您還可以使用HTTP HEAD、OPTIONS、TRACE和DELETE方法。 |
2 | 網絡服務 <mx:WebService>用於訪問與SOAP兼容的web服務的操作。 |
3 | 遠程對象 標記<mx:RemoteObject>用於表示MXML文件中的HTTPService對象。此標記允許您使用動作消息格式(AMF)編碼訪問Java對象的方法。 |
我們將詳細討論HTTP服務。我們將使用放置在伺服器上的XML源文件,並通過HTTP服務在客戶端訪問它。
Items.xml
<items> <item name = "Book" description = "History of France"></item> <item name = "Pen" description = "Parker Pen"></item> <item name = "Pencil" description = "Stationary"></item> <items>
HTTPService Declaration
現在聲明一個HTTPService並將上面文件的url傳遞給它
<fx:Declarations> <mx:HTTPService id = "itemRequest" url = "http://www.tutorialspoint.com/flex/Items.xml" /> </fx:Declarations>
RPC Call
調用itemRequest.send()方法並將itemRequest webservice的lastResult對象中的值綁定到Flex UI組件。
... itemRequest.send(); ... <mx:DataGrid id = "dgItems" height = "80%" width = "75%" dataProvider = "{itemRequest.lastResult.items.item}"> <mx:columns> <mx:DataGridColumn headerText = "Name" dataField = "name" /> <mx:DataGridColumn headerText = "Description" dataField = "description" /> </mx:columns> </mx:DataGrid>
RPC Service Call Example
現在,讓我們按照步驟在Flex應用程式中測試RPC服務;
Step | Description |
---|---|
1 | Create a project with a name HelloWorld under a package com.tutorialspoint.client as explained in the Flex - Create Application chapter. |
2 | Modify HelloWorld.mxml as explained below. Keep rest of the files unchanged. |
3 | Compile and run the application to make sure business logic is working as per the requirements. |
<?xml version = "1.0" encoding = "utf-8"?> <s:Application xmlns:fx = "http://ns.adobe.com/mxml/2009" xmlns:s = "library://ns.adobe.com/flex/spark" xmlns:mx = "library://ns.adobe.com/flex/mx" minWidth = "500" minHeight = "500" creationComplete = "init(event)"> <fx:Style source = "/com/tutorialspoint/client/Style.css" /> <fx:Script> <![CDATA[ import mx.events.FlexEvent; import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; protected function init(event:FlexEvent):void { itemRequest.send(); } ]]> </fx:Script> <fx:Declarations> <mx:HTTPService id = "itemRequest" url = "http://www.tutorialspoint.com/flex/Items.xml" /> </fx:Declarations> <s:BorderContainer width = "630" height = "480" id = "mainContainer" styleName = "container"> <s:VGroup width = "100%" height = "100%" gap = "10" horizontalAlign = "center" verticalAlign = "middle"> <s:Label id = "lblHeader" text = "RPC Service Demonstration" fontSize = "40" color = "0x777777" styleName = "heading" /> <s:Panel id = "parentPanel" title = "Using RPC Services" width = "500" height = "200" > <s:layout> <s:VerticalLayout gap = "10" verticalAlign = "middle" horizontalAlign = "center" /> </s:layout> <mx:DataGrid id = "dgItems" height = "80%" width = "75%" dataProvider = "{itemRequest.lastResult.items.item}"> <mx:columns> <mx:DataGridColumn headerText = "Name" dataField = "name" /> <mx:DataGridColumn headerText = "Description" dataField = "description" /> </mx:columns> </mx:DataGrid> </s:Panel> </s:VGroup> </s:BorderContainer> </s:Application>
完成所有更改後,讓我們以正常模式編譯並運行應用程式,就像我們在flex-create application一章中所做的那樣。如果您的應用程式一切正常,它將產生以下結果:[聯機試用]