Pages

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.


3 comments:

  1. Hey regarding this json web service call, how to ignore certificate warnings?

    https://forums.oracle.com/forums/thread.jspa?threadID=2466149

    ReplyDelete
  2. Hi Edwin.!

    Need a help!
    i have a 30mb xml file which has 3 ubounded elements into into
    like

    orderheaderdetils is just complex type
    order----- list----listof itemes

    in an order we get number of listitemnames(list)ubounded and listofitems(descriptions)ubounded

    the file size is increasing.. upto.. 30mb
    loading in bpel is itself consuming lot of time and processing to database is still more taking lot of time.

    and this cannot be controlled from senders end also..


    is there any idea that we can.. split before sending to bpel.

    coz we have many root elements we cannot batch it equally..

    so.. any help will be helpfull

    ReplyDelete
    Replies
    1. Hi,

      Can you use OSB and do a split join or try to do the same in a memory BPEL and use a for each parallel async invoke.

      thanks

      Delete