By Denis T on Jan 10, 2013
This article discusses and demonstrates the use of REST web services in your ADF Mobile applications.
First you should download the RESTDemo example application and install it and run it and take a look. The general usage of the app is that it takes a domain or IP address and returns you the geo-location of it. The app provides this in a form and then you can view it on a map as well. This demo uses a public web service provided by FreeGeoIP and it's author, Alexandre Fiori. NOTE: This service has a throttling mechanism of 1000 queries an hour so if you get a 403, you should wait until the next hour before you test again.
There are two flavors of REST supported by ADF Mobile. The first we'll talk about is REST XML. The nice thing about REST XML is that it's structure is defined by an XSD and thus the design-time knows that the format should be and can provide you with a shape for the data when building your app. You use the URL Data Control wizard from the new gallery to define a Data Control backed by a REST XML service. You need to supply an XSD that defines what the structure will be. Note: if you have a service that returns XML but you don't have an XSD, there are many free utilities that will generate the XSD for you. When specifying the endpoint, you should typically keep this to the minimum needed for all methods of your web service. You then specify the HTTP method for the method of your service you want to invoke. Note that if you have a single service that supports multiple methods, you should be selecting the same endpoint and just keep adding methods to it, DO NOT generate separate Data Controls for each method. Any extra info you need for specific methods, including parameters should be put into the Source field.
Once your DC is created, it can be used just like any other Data Control. You can optionally decide to invoke these Data Control methods from Java and follow the techniques outlines in the Web Service example #2 in this blog if you wish.
The REST XML Data Control for the Geo IP is used directly in the REST-XML feature and shows you how to declaratively use the DC without a bean.
Because of the loosely structured nature of REST JSON services, there is no declarative model available for most because they do not provide a consistent way to describe the shape of the data model. The way to use REST JSON services with ADF Mobile is to execute these services with our helper classes and then use the supplied JSON parsing classes to create beans from the data and bind your UI to those beans.
In the second feature, REST-JSON, we have a managed bean created called RESTJSONBean. This bean maintains the parameter that we send to the service method along with the JSON response that is returned.
Steps to invoke and parse the REST JSON web service:
- The bean method loadData first declares an instance of the RestServiceAdapter class. This is a helper class that lets you invoke REST web services and simply returns the results in string format.
- The RestServiceAdapter lets you set the connection name (from connection.xml), the request type (GET/POST/etc), what the retry limits are and then you can specify anything else you want added to the URI.
- The send method of the RestServiceAdapter is invoked and the service is called and the JSON string is returned.
- To easily parse the JSON string into a bean, we provide the JSONBeanSerializationHelper class. This class has a fromJSON method where you can specify the destination class type and the string to parse and it will return an object of the class you specified. In our case, we have defined a RESTJSONResponse class to hold our GeoIP information returned from the service.
Now that the service is invoked and the results are parsed into the bean, the UI is updated because it is already bound to the bean DC. With the REST JSON version of this you don't need a Data Control for the service, but you do need a connection in your connection.xml to store the URL connection. You can optionally bind the data directly to your bean or create a Data Control out of the Bean and bind that to the UI. Normally it's easier to do the latter once you understand the binding framework.
This pretty much completes this example. I've added the map just as a nice UI representation of geo-coordinate supplied by this service. In the future you will see some more structured REST JSON web services coming out of Oracle that have methods that provide schema information. This will allow us to use them declaratively within ADF Mobile.
Good luck and happy coding!