Part 2: Consuming a Web Service in an ADF Mobile Application
In this section of the tutorial, you take the application you have just completed and expand it by adding components. You add some view components to your application. You then define an ADF data control based on a web service. In the last section you create a page that uses the web service to update salary information for an employee.
Preparation Application

During this section of the tutorial, you create a data control that consumes a web service. The web service will run locally on your machine. You can download the JDeveloper Workspace that contains the web service right now. Later in this tutorial, you learn how to run the web service.

Download the compressed file using the download button to the right, and extract the file into a myWork directory.

Download SampleWS.zip
Step 1: Add View Components to the Application

    In this part of the tutorial, you add view components and navigation rules to the existing application. You will create the pages for these components in the next section.

  1. Open EmpsTaskFlow.xml in the editor.

    alt text
  2. Add a View Activity from the Component Palette to the task flow and name it SalaryUpgrade.

    alt text
  3. Drag another View Activity to the task flow and name it TakePicture.

    alt text
  4. Draw a Control Flow Case between empList and TakePicture. Name the flow takePicture.

    alt text
  5. Draw a Control Flow Case between EmpList and SalaryUpgrade. Name the flow goWS.

    alt text
  6. You now have the components on which you will base the remainder of the application.

Step 2: Define a Web Service Data Control

In this section, you open and run a web service that you will use in your application. You then create a data control based on the web service and consume the data control from your application.

  1. In the Preparation step of this tutorial, you downloaded SampleWS.zip and extracted it into a myWork directory of your choice. If you did not do that, go to the beginning of this tutorial and download and extract that file now.

  2. In JDeveloper click File > Open and navigate to the directory you used when you unzipped SampleWS.jws. Select SampleWS.jws and click Open.

    alt text
  3. In the Application Navigator, expand Project 1 > Application Sources > a to show Service.java. Right-click Service.java and select Run.

    alt text
  4. If this is the first time you have invoked the WebLogic Server (WLS), it will prompt you for a password. Enter a password, confirm the password, and click OK. If you are running WLS for the first time, it builds a default domain and might take a few minutes to start.

    alt text
  5. When the web service is deployed and running, you see a completion message in the Running messages tab. pane when the web service is deployed and is running. IMPORTANT NOTE: If the web service stops for any reason, you must restart the web service or your deployed mobile application will not function correctly. Specifically, you will see a failure when you try to update an employee's salary.

    alt text
  6. Click the link for the Target URL to open the HTTP Analyzer. You can use the HTTP Analyzer to test the web service. If you were to enter values in each of the parameters and click Send Request. The web service will process the request and return the results all from within the HTTP Analyzer.

    alt text
  7. Right-click the WSDL URL near the top of the Analyzer pane and select Copy. You will use this WSDL address when you create the Data Control.

    alt text
  8. Now that the web service is running, you switch back to the Employees workspace to use the web service.

  9. Use the drop down list in the Application Navigator to navigate back to the Employees workspace.

    alt text
  10. Right-click the ViewController project and select New.

    alt text
  11. In the New Gallery, select Business Tier > Data Controls > Web Service Data Control and click OK.

    alt text
  12. If you deploying to a device or an emulator, you need to specify the IP address of the machine on which the web service is running.

    1. To find the IP address on a Windows machine, start a command window (select Start > Run, enter CMD, and then click OK.)

      alt text
    2. In the command window, type ipconfig and press enter.

      alt text
    3. Copy the IP address of the machine. Use that IP Address in the URL of the web service data control. THe image below has the I.P. address blacked out for security purposes.

      alt text
  13. In the Create Web Service Data Control wizard, name the web service data control SalaryService, and paste the URL of the WSDL into the URL field. Click Next.

    Again, for security purposes, the I.P. address is not visable in the image below.

    alt text
  14. Select the SalaryUpgrade method, shuttle it to the Selected list, and click Finish.

    alt text
  15. Save your work.

  16. Open the Data Controls palette and click Refresh to see the new web service data control, SalaryService.

    alt text
  17. You now have a Data Control that is based on the SalaryService web service. In the next few steps, you create the SalaryUpgrade page for the view activity you added earlier. You also add navigation to the EmpsList page.

Step 3: Consume a Web Service Data Control

In ADF, the steps for consuming all data controls are the same. It doesn't matter if the data control is based on a Java class, an ADF Business Component, or a web service. In this section, you add elements to your application that consume the Salary Service data control.

  1. Open EmpsList.amx in the editor.

    alt text
  2. As you did earlier, use the splitter in the editor to view both the code and the preview of the page.

    alt text
  3. Choose iPad as the device type so you can see more detail on the screen while you are working on the page.

    alt text
  4. In the source code editor, select the button with the Id cb2 (the right-most button on the page). I the Property Inspector, set the Text to Update and the Action to goWS.

    alt text
  5. Save your work.

  6. Next, you will design the second page, SalaryUpgrade which you will use to consume the web service.

  7. Open EmpsTaskFlow.xml in the editor.

    alt text
  8. Double-click SalaryUpgrade to create the page. Click OK to accept the default values for the new page.

    alt text
  9. There will be two parts to this page. The first part will show the employee data for the currently selected row. The second part will be used to invoke the web service.

  10. To show the employee data, drag the emps collection to the Panel Page and drop it as an ADF Mobile Form.

    alt text
  11. Remove all the attributes except for Name and Salary and click OK to add the form to the page.

    alt text
  12. Click the Bindings tab at the bottom of the editor to see that the bindings for Name and Salary are added to the page.

    alt text
  13. This data control and bindings will show employee data, but you need to make sure that the page displays the data of the currently selected row. To select the current row, you add a call to a method that will set the row.

  14. To ensure that you have the row you want, add an Action binding that will call the method. Click the Add icon (the green plus sign ) on the Bindings panel.

    alt text
  15. Select action as the binding type and click OK.

    alt text
  16. In the Create Action Binding editor, select the empsIterator and set the Operation to setCurrentRowWithKeyValue.

    alt text
  17. Set the Value property of the rowKey parameter to #{pageFlowScope.empId}. Remember that you created this variable to store the current rowKey from the previous page, empList.amx.

    alt text
  18. Now that you have the action binding, you need to add it to the list of executables.

  19. Click the Add icon (the green plus sign) on the Executables panel.

    alt text
  20. Select invokeAction as the type and click OK.

    alt text
  21. Set the id the setEmpId and Binds to setCurrentRowWithKeyValue. Click OK.

    alt text
  22. Notice that the invokeAction is the last executable in the list. This action sets the current row in the iterator, so it should be the first action to be executed.

    alt text
  23. Drag setEmpId to the top of the executables list.

    alt text
  24. In the next few steps, you add the components that call the web service.

  25. Save your work.

  26. Click the Source tab on the SalaryUpgrade.amx page.

  27. From the SalaryService data control, drag the SalaryUpgrade method to the Panel Page and drop it as an ADF Mobile Button.

    alt text
  28. The Salary Upgrade web service accepts the following parameters; Name, Date, Salary, and Commission. Name and Salary are fields on the form you just added. Therefore, when you call the the web service, you pass the Name and Salary from the fields on that form.

  29. Click the down arrow next to the Value property of the name parameter and click Show EL Expression Builder.

    alt text
  30. Use the Expression Builder to select bindings.name.inputValue and click OK. (The expression gets the current value of the name field on the form.)

    alt text
  31. Repeat those steps for the sal field. (HINT: In the Expression Builder, select bindings.salary.inputValue and click OK.)

    alt text
  32. For the next two parameters you set the values to viewScope variables you populate later.

  33. Using the same process as before, select the date parameter and set the Value to #{viewScope.start}

    alt text
  34. Using the same process as before, select the comm parameter and set the Value to #{viewScope.commission}.

    alt text
  35. Click OK to create the button.

    alt text
  36. Save your work.

  37. Click the bindings tab to examine the bindings you just added.

    alt text
  38. Expand SalaryUpgrade in the Structure pane to see the parameters.

    alt text
  39. Click each of the parameters and notice in the Property Inspector that you can see the ND values for each of the parameters.

    alt text

    alt text
  40. As you have seen, the SalaryUpgrade web service accepts four parameters. The first two values come from the form you created from the emps data control. That leaves two more values that need to come from somewhere. You have already set the bindings on the SalaryUpgrade method call to use the two form fields. Now you need to create fields to accept the two additional parameter values (date and commission) and store those values in the viewScope parameters that you created.

  41. Click the Source tab on the SalaryUpgrade page and drag a Switch (boolean) component from the Component Palette to the Panel Form Layout component in the Structure window.(HINT: The Switch component is under Text and Selection in the Component Palette)

    alt text

    alt text
  42. In the Property Inspector, set the label to Commissioned? and the Value to #{viewScope.commission}.

    alt text

  43. Drag an Input Date component to the Panel Form Layout.

    alt text

    alt text
  44. In the Property Inspector, set the label to Starting and the Value to #{viewScope.start}.

  45. That takes care of the parameter values passed to the web service, but we also want to show the results from the web service.

  46. Expand the SalaryService data control to see the String, which represents the results from the web service.

    alt text
  47. Drag String to the Panel Page and drop it as an ADF Mobile Output Text.

    alt text
  48. At this point, the page is complete and would run. However because the output field is based on a call to the web service, the page will call the web service before the user can enter the start date or the commission value. In the next few steps you set the page to call the web service only after the SalaryUpgrade button is clicked.

  49. First, you want to control the rendering of the Output Text field that holds the results from the web service. To do that, add a condition to the Rendered property. Using the Expression Builder, set the Rendered property to evaluate a new variable, #{viewScope.displayResults == 'Y'}. Click OK.

    alt text
  50. Next, add a setPropertyListener component that sets the value of the viewScope.displayResults variable to Y. To do this, drag a SetPropertyListener component to the SalaryUpgrade button, and set the following properties:
        From: #{'Y'}
        To: #{viewScope.displayResults}
        Type: action

    alt text

    alt text

    alt text
  51. Finally, you need to do provide a way back to the calling page. Select the first button, which is cb1 and in the Property Inspector, set the Text to Back and the Action to __back.

    alt text

    alt text
  52. Save your work.

  53. As a result of the previous few steps, when the user enters values in the commission and date fields and clicks the SalaryUpgrade button, the page calls the web service and displays the results.

    In preparation for the completed application, you created a view activity on EmpsTaskFlow called TakePicture. In Part 3 of this tutorial, you will use that view activity to use device services. However, before you can deploy the application in this part of the tutorial, you need to create a page as a place holder for that view activity.

  54. Create a default empty page for the TakePicture view activity using the default values. (HINT: Open EmpsTaskFlow, double-click TakePicture and click OK to accept the default values for the page.)

    alt text
  55. Save your work.

  56. Next, deploy and test the application

  57. From the Application menu in JDeveloper, select Deploy Anfroid2 to Android emulator.

    alt text
  58. In the Deployment tab of the Log window, make sure the deployment is finished.

    alt text
  59. Remember that the SalaryUpgrade web service needs to be running for the application to work. The instructions for running the web service are in step 2 of this tutorial.

  60. In the emulator, or your device, navigate to the Applications page and open the Emps application.

    alt text
  61. The emulator now shows the Emp List page which is the entry page to your application.

    alt text
  62. Click the right-arrow on the left side of the page to show the list of employees.

    alt text
  63. Click Shay (or any of the employees in the list) to select an employee.

    alt text
  64. Click the Update button. The application now shows the SalaryUpgrade page.

    alt text
  65. Using the Android Emulator UI, change the salary to 5555, set the Commission to On, and change the Start Date to a few days from today.

    alt text
  66. Click SalaryUpgrade. Notice the message that appears under the SalaryUpgrade button. This message is returned from the SalaryUpgrade web service. Your application has successfully called a web service and displayed the returned values.

    alt text

You have now successfully added ADF controls to your application that consume a web service. In the next section of this tutorial, you integrate some native device features into your application.

Bookmark Print Expand all | Hide all
Back to top
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.