By Denis T on Nov 02, 2012
In this edition of the ADF Mobile blog we'll tackle part 1 of our Web Service examples. In this posting we'll take a look at using a declarative SOAP Web Service.
Getting the sample code:
Just click here to download a zip of the entire project. You can unzip it and load it into JDeveloper and deploy it either to iOS or Android. Please follow the previous blog posts if you need help getting JDeveloper or ADF Mobile installed.
Defining our Web Service:
First off, we should mention that this sample code is using a public web service provided free by CDYNE Corporation that provides weather forecasts by zipcode. Sometimes this service goes down so please ensure you know it's up before reporting this example isn't working.
Let's take a look at the web service. We created this by using the "Web Service Data Control" from the New Gallery and using this link to this wsdl: "http://wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL" This web service has several methods but we're interested in GetCityForecastByZIP which takes a single string parameter for the zipcode and the second method, GetWeatherInformation that enumerates all possible forecast descriptions and associated image URLs. The latter we'll use in the next edition but we included it here for completeness.
Defing the Application:
After adding a feature to the adfmf-feature.xml file, we added a taskflow to host the application flow. This comprises of a home screen with a list with items for each method in the web service, "Forecast by Zip" and "Weather Info". In this application we've also decided to hide the navigation bar since there is only one feature in the application.
Forecast by Zip:
The "Forecast By ZIP" option first presents the user with a screen where they can enter a zipcode and when the "Search" button is tapped, it executes the GetCityForecastByZIP method. This is done by binding an Action binding to that method. The easiest way to accomplish this is to just drag & drop the method from the Data Control palette to the AMX page and drop it as a button and let the framework hook it up for you. There is an inputText component on the page that is bound to a pageFlowScope variable called "zip". This is used as the parameter to the Action binding when it is executed. Because the actionListener attribute of the commandButton executes the Web Service each time, we ensure that the method is invoked every time the button is clicked.
Unlike the previous method, this time instead of explictly executing the web service method we are using deferred invocation. What this means is that we will bind to the results of the method and the framework will execute the method when it the data is required to be rendered. We do this by simply doing a drag & drop of the results of the GetWeatherInformation to the AMX page. When the page is rendered and the bindings are resolved the framework invokes the method. This executes the method only when it is needed and fills the Data Control provider. Because we never re-execute the method, you can click from Home to Weather Info and back many times and the web service is only ever invoked once.
Issues and Possible Improvements:
One thing you will quickly realize with this example is that the error handling is done by the framework for you. For simple examples this is fine but for real applications you'll want to customize these error messages. With the declarative invocation of web services, this is difficult. This is one aspect we'll address in the second installment of the web service examples where we will show you how to do programmatic invocation which allows you better error handling.
Another issue you will notice with this example is that we can enumerate the weather information but there isn't an easy way to use that information to show the corresponding description and image as part of the forecast results. We'll show you how to do this in the next example.