ADF Mobile: Programmatic invocation of REST XML service for non UTF-8 encoded data

Recently, during my work on some use case, where I was implementing REST service invocation in ADF Mobile application, I have meet with problems that REST Data Control on ADF Mobile currently does not support XML data in encoding other than UTF-8. The solution of this issue is using programmatic approach for REST XML Service invocation. The stages of this approach are the following:

1) Create URL Connection in JDeveloper (File -> New -> Connections -> URL Connection), where specify URL Endpoint of your REST XML service. In our case I have used web service of Bank of Russia which returns daily currency rates in XML format in Cp1251 encoding (http://www.cbr.ru/scripts/XML_daily.asp).

2) In order to hold objects data in ApplicationController project - you should create Data Object Java class, which is defined to be a data holder for the object. It represents a single «row» of data and defines the attributes with appropriate getters/setters. In my case I have used Currency class, where I have defined variables which hold currency rates information.

public class Currency {

    private String NumCode;

    private String CharCode;

    private String Nominal;

    private String Name;

    private String Value;

// Setters/getters and Constructor here

}

3) In ApplicationController project you should also create Service Object Java class (which will be used later for Data Control creation) and specify programmatic service invocation here using RESTServiceAdapter class within get method;

RestServiceAdapter restServiceAdapter = Model.createRestServiceAdapter();

// Clear any previously set request properties, if any

restServiceAdapter.clearRequestProperties();


// Set the connection name
restServiceAdapter.setConnectionName("CBRF");

// Specify the type of request    restServiceAdapter.setRequestType(RestServiceAdapter.REQUEST_TYPE_GET);
restServiceAdapter.addRequestProperty("Content-Type", "application/xml");
restServiceAdapter.addRequestProperty("Accept", "application/xml; charset=windows-1251");

// Specify the number of retries
restServiceAdapter.setRetryLimit(0);

// Set the URI which is defined after the endpoint in the connections.xml.
// The request is the endpoint + the URI being set

restServiceAdapter.setRequestURI("");

4) It is necessary to get response exact in array of byte format, so you should use sendReceive method for these purposes here;

byte[] response = null;

response = restServiceAdapter.sendReceive("");

5) After receiving array of bytes in encoding other than UTF-8, it is necessary to transform it in UTF-8 string. At this stage you should use your specific implementation of transformation array of byte in national encoding (in your case) into Java string (in UTF-8). In my case it is Cp1251 transformation using custom decodeCp1251 method.

// Byte array into UTF8 string transformation.

String responseString = "";

responseString = decodeCp1251(response);

6) On previous step you have got UTF8 string with XML response and now you are ready to parse it programmatically using kXML library in order to fill ArrayList structure. In my case it is list of Currency objects.

KXmlParser parser = new KXmlParser();

parser.setInput(new InputStreamReader(new ByteArrayInputStream(responseString.getBytes())));

parser.nextTag();

parser.require(XmlPullParser.START_TAG, null, "ValCurs");

while(parser.nextTag() == XmlPullParser.START_TAG) {

parser.require(XmlPullParser.START_TAG, null, "Valute");

while(parser.nextTag() == XmlPullParser.START_TAG) {

parser.require(XmlPullParser.START_TAG, null, "NumCode");

// handle element content

String NumCode = parser.nextText();

parser.require(XmlPullParser.END_TAG, null, "NumCode");

parser.nextTag();

parser.require(XmlPullParser.START_TAG, null, "CharCode");

// handle element content

String CharCode = parser.nextText();

parser.require(XmlPullParser.END_TAG, null, "CharCode");

parser.nextTag();

parser.require(XmlPullParser.START_TAG, null, "Nominal");

// handle element content

String Nominal = parser.nextText();

parser.require(XmlPullParser.END_TAG, null, "Nominal");

parser.nextTag();

parser.require(XmlPullParser.START_TAG, null, "Name");

// handle element content

String Name = parser.nextText();

parser.require(XmlPullParser.END_TAG, null, "Name");

parser.nextTag();

parser.require(XmlPullParser.START_TAG, null, "Value");

// handle element content

String Value = parser.nextText();

parser.require(XmlPullParser.END_TAG, null, "Value");

currencyList.add(new Currency(NumCode, CharCode, Nominal, Name, Value));

}

parser.require(XmlPullParser.END_TAG, null, "Valute");

}

parser.require(XmlPullParser.END_TAG, null, "ValCurs");

currency = (Currency[]) currencyList.toArray(new Currency[currencyList.size()]);

For more information, please consult with kXML documentation at:

· http://kxml.sourceforge.net/kxml2

· http://kxml.sourceforge.net/kxml2/javadoc

7) So, you have got ArrayList structure with objects data (in my case it is Currency objects each of them holds currency rate information). Now you should wrap it into data control artifact. In order to do it, in Application Navigator find out your Service Object Java class, right click on it and select Create Data Control option.

Finally in Data Controls window you will see your data control artifact with data collection inside it (in my case it is CurrencyRatesService data control with currencyRates data collection).

8) Drag data collection from data control and drop it into newly created AMX page as listView AMX component.

9) Deploy your application on emulator or device and run it.

As I have mentioned above, in my case I have used Bank of Russia web service which returns currency rates values so, I have created List View in Main-Sub text format with some divider settings in order to make list of currency rates more informative:

Finally, I have made my application a little bit complex having added taskFlow with list and detail pages describing currency rates:

You could download JDeveloper application demo project here.

Russian-speaking readers could find this article on Russian language in Middleware Technology blog for Russian developer’s community.

Be happy with your ADF Mobile application! 

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Oracle ECEMEA Partner Hubs
Migration Center Team


We share our skills to maximize your revenue!
Adopt and implement the latest of Oracle Technology in your solutions.

Stay Connected
partner.imc
@
beehiveonline.oracle-DOT-com
Google+ Subscribe

Search

Archives
« September 2015
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today