May 25, 2021 Flex
Flex provides RPC services to provide server-side data to clients. /b10> Flex provides considerable control over server-side data.
With the Flex RPC service, we can define the user actions to be performed on the server side.
Flex RPC services can be integrated with any server-side technology.
One of the Flex RPC services provides built-in support for compressed binary data transmitted over the line and is very fast.
Flex offers three types of RPC services
RPC services | Describe |
---|---|
HttpService | The httpService label is used to represent the HTTPService object in the MXML file. /b11> When you call the send() method of the HTTP Service object, it makes an HTTP request to the specified URL and returns the HTTP response. Y ou can also use the HTTP HEAD, OPTIONS, TRACE and DELETE methods. |
WebService | The webservice tag is used to access SOAP-compliant Web services. |
RemoteObject | The remoteObject tag is used to represent the HTTPService object in the MXML file. /b11> This tag enables you to encode methods for accessing Java objects using the Action Message Format (AMF). |
We'll discuss HTTP services in more detail. We will use the XML source file placed on the server and access it on the client side through the HTTP service
<items> <item name="Book" description="History of France"></item> <item name="Pen" description="Parker Pen"></item> <item name="Pencil" description="Stationary"></item> <items>
Now declare an HTTP Server and pass the url of the above file
<fx:Declarations> <mx:HTTPService id="itemRequest" url="//www.w3cschool.cn/flex/Items.xml" /> </fx:Declarations>
Call the itemRequest.send() method and bind the value from the itemRequest webservice's lastResult object to the Flex UI component.
... 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>
Now let's test the RPC service in a Flex application by following these steps:
Steps | Describe |
---|---|
1 | As described in the Flex - Create Applications section, create a project called HelloWorld under package com.tutorialspoint.client. |
2 | Modify HelloWorld.mxml, as described below. /b10> Keep the rest of the file unchanged. |
3 | Compile and run the application to ensure that the business logic works as required. |
The following is the contents of the modified mxml file src / com.tutorialspoint / HelloWorld.mxml.
<?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="//www.w3cschool.cn/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>
When all the changes are ready, let's compile and run the application in normal mode, as in the Flex - Create Application chapter. If all goes well with your application, this will result in the following results: