Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using Web Services with Data Centric Development in Flash Builder 4 beta

DZone's Guide to

Using Web Services with Data Centric Development in Flash Builder 4 beta

· Web Dev Zone
Free Resource

Tips, tricks and tools for creating your own data-driven app, brought to you in partnership with Qlik.

Data-centric development is an exciting new Rapid Application Development (RAD) feature in Flash Builder 4 beta 2, which helps traditional web and novice Flex developers to quickly build rich data-centric Flex applications that fetch data from various back-ends including ColdFusion, PHP, BlazeDS, LiveCycle Data Services ES, web services, and HTTP services.

Because web services are such a popular mechanism for exchanging data between different applications and platforms on the Internet, Flash Builder 4 provides easy and efficient ways to import, explore, and use web services. Through its unified workflow wizards, Flash Builder 4 helps developers quickly build Rich Internet Applications that leverage existing corporate or public service infrastructures, including web services.

In this article, I will demonstrate these capabilities by showing you how to build an application (see Figure 1) that uses Zenfolio's web service to display photos.

The sample application

Figure 1. The sample application

Requirements

In order to make the most of this article, you need the following software and files:

Flash Builder 4 beta 2

Sample files:

Prerequisite knowledge

Previous experience with Flex Builder, Flash Builder, or web services will be helpful, but it is not necessary.

Importing the web service

Note: The sample file ZenfolioProject.fxp contains the complete source code for the project. Following the steps in this tutorial will result in a complete application, but you can download the sample file as a reference.

Follow these steps to create a new Flex project and import the Zenfolio web service.

  1. Choose File > New > Flex Project
  2. Type a name for the project, for example ZenfolioApp.
  3. For the Application Type select Web, and for the Application Server Type select None/Other.
  4. Click Finish.
  5. Choose Data > Connect To Web Service.
  6. In the dialog box that appears, type http://www.zenfolio.com/api/1.0/zfapi.asmx?WSDL as the WSDL URI. This is the URI of the WSDL file for Zenfolio's web service.
  7. For the Service Name, type ZenfolioService. This is the name that you can use to refer to this service within the current project.
  8. For the Service Package, type services.zenfolioservice. This is the name of the package (folder) in which Flash Builder will generate the required ActionScript classes.
  9. For the Data Type Package, type valueObjects. This is the name of the package in which Flash Builder will generate the required ActionScript files for the custom data types used by this service.
  10. Click Next.

    Flash Builder will introspect the WSDL file and present a list of services, ports, and operations supported by the service.

    Note: A single WSDL file can contain multiple ports for different endpoints exposed over different protocols. The web service wizard in Flash Builder 4 supports the SOAP 1.1 protocol. For this particular WSDL file, the selected port will be ZfApiHttpGet, and as a result you'll see the following notification in the dialog box, “Selected WSDL port is not compliant with SOAP 1.1 protocol. Operations and Entities cannot be read from this port. Please select a different port.”

  11. Select ZfApiSoap as the Port.

    You are presented with a list of web service operations that you can use in this service.

  12. Click Select All for now, and then click Finish.

That's all there is to it. You have just finished importing the Zenfolio web service into your Flex project.

Exploring the web service

Follow these steps to explore the web service you just imported.

  1. Choose Window > Data/Services to display the Data/Services view.
  2. In the Data/Services panel, expand the ZenfolioService service to see the Data Types node and a list of operations. The operations are listed with input argument names, argument types, and return types. You can also expand Data Types to see the custom data types used by the service and the corresponding data structure.
  3. Locate GetPopularPhotos(), the method you will use in this tutorial (see Figure 2).

The GetPopularPhotos() method takes two int parameters, offset and limit, and returns an array of Photo objects.

The Photo custom data type is defined inside the service, and you can see its data structure under the Data Types node of ZenfolioService or by expanding any operation that returns a Photo type.

Web service operations in the
Data/Services panel

Figure 2. Web service operations in the Data/Services panel

Connecting components to data

Now that you've seen the operations provided by the web service, you can create a simple UI that will use those operations.

The UI should include the following components:

  • A List to display the list of popular photos with ID lstPhotos
  • An Image to display the high resolution version of the selected image with ID imgPhoto
  • A NumericStepper to enable the user to set the number of photos to display per page
  • A Header and appropriate background color (for example #787878) to make the application attractive

A basic UI for the sample application

Figure 3. A basic UI for the sample application

  1. You can create the UI shown in Figure 3 by copying the following code into ZenfolioApp.mxml:

    <s:Rect radiusX="8" radiusY="8" height="67" width="194" x="11"
    y="17" fill="{new mx.graphics.SolidColor(0x000000,1)}"/>
    <s:Label x="21" y="34" text="Zenfolio" color="#FFFFFF" fontSize="42"/>
    <s:Rect radiusX="8" radiusY="8" height="36" width="153" x="181"
    y="48" fill="{new mx.graphics.SolidColor(0x000000,1)}"/>
    <s:Label x="189" y="58" text="Popular Photos" color="#00DEFF" fontSize="20"/>
    <s:Line y="100" x="0" width="100%" stroke="{new
    mx.graphics.Stroke(0xFFFFFF,1)}"/>
    <s:List id="lstPhotos" x="10" width="152"
    contentBackgroundColor="#787878"
    borderAlpha="0" bottom="10" top="120">
    </s:List>
    <s:Group bottom="10" right="10" left="170" top="120">
    <mx:Image id="imgPhoto" horizontalCenter="0" verticalCenter="0"/>
    </s:Group>
    <s:Group height="35" width="152" top="57" right="10">
    <s:Rect radiusX="8" radiusY="8" width="100%"
    height="100%" fill="{new mx.graphics.SolidColor(0x000000,1)}"/>
    <s:Label x="9" text="Per Page" fontSize="14" color="#FFFFFF" y="11"/>
    <s:NumericStepper id="nsResults" x="76" minimum="10"
    maximum="200" stepSize="5" value="50" y="6"/>
    </s:Group>
  2. Switch to Design view.
  3. To display the Photo array returned by GetPopularPhotos() in the List component, drag-and-drop the GetPopularPhotos() operation from the Data/Services panel to the List component.

    Note: You can use several alternative approaches for this step. You can right-click the List component and select Bind To Data from the context menu. Or, you can select the List component and then click the Bind To Data icon next to the Data Provider in the Properties panel or click the Bind To Data icon in the Data/Services panel.

  4. In the Bind To Data dialog box that appears, select GetPopularPhotos as the Operation and select Title as the Bind To Field setting.
  5. Click OK.
  6. After Flash Builder 4 generates the required code and switches to Source view, type 1,nsResults.value as the parameters to the GetPopularPhotos() call as shown below:
protected function lstPhotos_creationCompleteHandler(event:FlexEvent):void
{
GetPopularPhotosResult.token =
zefolioService.GetPopularPhotos(1,nsResults.value);
}

The generated code in ZenfolioApp.mxml calls the web service operation on the creationComplete event of the List component.

The GetPopularPhotos() operation expects two arguments, and you can pass static values or bind them to other UI components. In this case, you are passing 1 as the offset and nsResults.value (the value set in the NumericStepper control) as the limit.

Defining interactions

If you run the project (choose Run > Run ZenfolioApp), you'll see that the photo titles are displayed in the List control, but no photos are displayed even if you select them.

Note: Calls to the web service may take several seconds to complete; some patience is required.

Follow these steps to add a change event handler on the List control that will display the image whenever the user selects an item.

  1. In Design view, right-click the List component and select Generate Change Handler from the context menu.
  2. Edit the lstPhotos_changeHandler() method that was generated as shown below to get the URL of the selected image and assign it to imgPhoto.source:

    protected function lstPhotos_changeHandler(event:IndexChangeEvent):void
    {
    imgPhoto.source = "http://www.zenfolio.com/"
    + lstPhotos.selectedItem.UrlCore + "-4.JPG";
    }
  3. Choose Run > Run ZenfolioApp, and you will see the high-resolution image displayed when you select an item from the list.
  4. To display a thumbnail along with the title of the photo as shown in Figure 1, add an itemRenderer for the List control. In Source view, replace the code for the existing List control with the following MXML code:
<s:List id="lstPhotos" x="10" width="152" contentBackgroundColor="#787878"
borderAlpha="0" bottom="10" top="120"
creationComplete="lstPhotos_creationCompleteHandler(event)"
dataProvider="{GetPopularPhotosResult.lastResult}" labelField="Title"
change="lstPhotos_changeHandler(event)">
<s:itemRenderer>
<fx:Component>
<s:ItemRenderer height="100" width="90%" buttonMode="true">
<s:Group height="90" width="90%">
<s:Rect radiusX="8" radiusY="8"
width="100%" height="100%"
fill="{new mx.graphics.SolidColor(0x000000,1)}"/>

<mx:Image y="5" source="{'http://www.zenfolio.com'
+ data.UrlCore + '-1.jpg'}"
horizontalCenter="0"/>

<s:Label text="{data.Title}"
y="74" color="0xFFFFFF" width="100%" height="12"
textAlign="center"/>
</s:Group>
</s:ItemRenderer>
</fx:Component>
</s:itemRenderer>
</s:List>

Where to go from here

Data-centric development in Flash Builder 4 helps developers quickly build Rich Internet Applications through unified workflows across various back-end technologies. It enables novice developers to get started quickly, and helps experienced developers be more productive. Data-centric development also makes advanced development methods like client side data management, paging, form generation, and chart generation easier for all Flex developers.

For more information on DCD see my introductory blog post on the topic and Tim Buntel's article Data-centric development with Flash Builder 4 beta. Sujit Reddy G also covers DCD in Flash Builder 4 on his blog.

As next steps for the sample application, you may want to add an event handler to the NumericStepper control to implement the results per page feature, or add Next and Previous buttons to navigate to all the photos from Zenfolio.

Explore data-driven apps with less coding and query writing, brought to you in partnership with Qlik.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}