Pages

Showing posts with label adf mobile. Show all posts
Showing posts with label adf mobile. Show all posts

Sunday, October 28, 2012

Using JSON-REST in ADF Mobile

In the current version of ADF Mobile the ADF DataControls ( URL and WS ) only supports SOAP and JSON-XML. But this does not mean we cannot use JSON. To handle JSON we can use the  RestServiceAdapter and JSONBeanSerializationHelper classes. The RestServiceAdapter will handle the Rest Service and JSONBeanSerializationHelper helps us converting JSON to Java.

I made a little ADF Mobile demo based on the Google Maps Geocoder and use this url to test it
http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true

with this as result


We start by creating an Url Connection with http://maps.googleapis.com/maps/api/geocode/json as value

Next is a new Class which can be used a managed bean or as a Java DataControl.

Here we do the following steps.

Create the RestServiceAdapter

RestServiceAdapter restServiceAdapter = Model.createRestServiceAdapter();
restServiceAdapter.clearRequestProperties();

Use our Url Connection
restServiceAdapter.setConnectionName("GoogleGeocodeJSON");

HTTP Get operartion
restServiceAdapter.setRequestType(RestServiceAdapter.REQUEST_TYPE_GET);

Append the url with our search parameters
restServiceAdapter.setRequestURI("?address="+search+"&sensor=true");

Send and wait for the result.
response = restServiceAdapter.send("");

Next step is using the JSON deserialization, here we will use the JSONBeanSerializationHelper class.
ServiceResult responseObject = (ServiceResult)jsonHelper.fromJSON(ServiceResult.class, response); 

ServiceResult class will be used as output, too bad I can't use generics or annotations to control the JSON deserialization. So I will use JSONArray in case of 1 or more results.

import oracle.adfmf.json.JSONArray;
public class ServiceResult {
   private String status;
   private JSONArray results;

JSONBeanSerializationHelper will look for attributes called .type and if that contains a class name then it will use that class for deserialization but I can't change the Google Maps service.

So I made my own Helper class which converts all JSONArray or JSONObject to the right class and attributes.

geoResult = GeocoderHelper.transformObject(responseObject).getResults();

Here is my the helper class


Now we are ready to use it in a managed bean or generate an ADF DataControl on it.
with this as result.

You can find my demo code on Github https://github.com/biemond/jdev11gR2_examples/tree/master/MapsMobileRest

Here the pictures of the application in the IPad emulator.