The Mobile & Digital Assistant Blog covers the latest in mobile and conversational AI development and engagement

ADF Mobile REST JSON/XML example

Guest Author

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:

  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!





Join the discussion

Comments ( 8 )
  • guest Friday, January 11, 2013

    Deployment gives the following error

    [04:42:37 PM] Failed to create the Java Bean Definition XML files for the "GeoipDC" data control due to an invalid schema or an invalid schema URL.

    [04:42:37 PM] Shutting down Android Debug Bridge server...

    [04:42:37 PM] Deployment cancelled.

    [04:42:37 PM] ---- Deployment incomplete ----.

    [04:42:38 PM] Failed to create the Java Bean Definition XML files.

    [04:42:38 PM] DCA-29000: Unexpected exception caught: oracle.adf.model.adapter.AdapterException, msg=DCA-29000: Unexpected exception caught: oracle.xml.parser.v2.XMLParseException, msg=Start of root element expected.

    [04:42:38 PM] DCA-29000: Unexpected exception caught: oracle.xml.parser.v2.XMLParseException, msg=Start of root element expected.

    [04:42:38 PM] Start of root element expected.

  • DenisT Tuesday, January 15, 2013

    Sorry, I see there was an absolute reference to the XSD that used my local drive. If you go into ViewController/adfmsrc/DataControls.dcx you can hand-edit the reference to the XSD to: SchemaLocation="/src/freegeoip.xsd". The zip has been updated with the fix already. Sorry for the confusion.

  • Srini Friday, March 15, 2013

    Thanks for the blog. I have deployed the app on Android emulator and I am getting the following error when I hit search button:

    Cannot connect ot freegeoipnet on port 80: java.net.UnKnownHostException

  • Nicole Friday, June 21, 2013

    I get the same error on iOS. Cannot connect to freegeoip.net on port 80: java.net.SocketException: Network is unreachable.

    I can access the URL (http://freegeoip.net/json/oracle.com) in safari on the simulator though. So why can't the application access the URL?

  • guest Friday, July 19, 2013

    I have created a rest service and deployed it on my weblogic server inbuilt with Jdeveloper. This rest service is working fine. But when i try to create a Data Control in my mobile application by providing the URI for this rest service(http://localhost:7001/RestService), test connection is never successful. Even if i deploy the application in Andorid Emulator, i am always getting the java.net.connect exception. Please help.

  • DenisT Friday, July 19, 2013

    1) You should use the actual IP of your service and not "localhost". The device/emulator might have it's own localhost defined.

    2) Make sure you have any required proxies setup correctly. You'll have to refer to the emulator help to define proxies.

  • Ankur Wednesday, July 24, 2013

    Using IP Address in place of localhost worked. Thankyou Denis for your help!.

  • Dhondi Sunday, October 18, 2015

    I get the same error on android. Cannot do POST method

    POST method working in safari on the simulator though. But not working on the application ?

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha