Thursday Jan 10, 2013

ADF Mobile REST JSON/XML example

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.

REST XML

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.  

REST JSON

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:

  1. 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. 
  2. 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. 
  3. The send method of the RestServiceAdapter is invoked and the service is called and the JSON string is returned.
  4. 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.

What's next?

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!




ADF Mobile applications built on Oracle Forms!

Yes, it's true!  You can build ADF Mobile applications built against an Oracle Forms back end!  One of Oracle's partners, OraPlayer,  has built a recorder/player that can turn a forms app into a series of web services and a corresponding client proxy class for use with ADF Mobile.  You don't have to rewrite any server code and you get extended use and capabilities out of your existing Forms apps.

Here is a great blog article with a demo of the technology.

Join us for a joint Webinar with the OraPlayer folks on February 7th, 9-10am Pacific.  

This is a great way to create new state-of-the-art mobile applications without retooling your Forms applications!

About

This blog is is dedicated to announcements,tips and tricks and other items related to developing, integrating, securing, and managing mobile applications using Oracle's Mobile Platform. It is created and maintained by the Oracle Mobile product development team.

Archive of past entries

Even More Mobile Development Blogs

Oracle A-Team Site - Mobile Related Entries

Code samples from the Community

Fusion Middleware Blogs

Search

Archives
« January 2013 »
SunMonTueWedThuFriSat
  
1
2
4
5
6
8
9
12
13
14
15
16
17
18
19
20
21
22
23
25
26
27
28
29
30
31
  
       
Today