7/31/2019 Android+ +How+to+Guide
1/31
Consuming SAP NetWeaver Gateway
Services from Android Applications
7/31/2019 Android+ +How+to+Guide
2/31
Copyright/Trademark
Copyright
Copyright 2011 SAP AG. All rights reserved.
SAP Library document classification: PUBLIC
No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission
of SAP AG. The information contained herein may be changed without prior notice.
Some software products marketed by SAP AG and its distributors contain proprietary software components of othersoftware vendors.
Microsoft, Windows, Excel, Outlook, and PowerPoint are registered trademarks of Microsoft Corporation.
IBM, DB2, DB2 Universal Database, System i, System i5, System p, System p5, System x, System z, System z10, Systemz9, z10, z9, iSeries, pSeries, xSeries, zSeries, eServer, z/VM, z/OS, i5/OS, S/390, OS/390, OS/400, AS/400, S/390Parallel Enterprise Server, PowerVM, Power Architecture, POWER6+, POWER6, POWER5+, POWER5, POWER,OpenPower, PowerPC, BatchPipes, BladeCenter, System Storage, GPFS, HACMP, RETAIN, DB2 Connect, RACF,Redbooks, OS/2, Parallel Sysplex, MVS/ESA, AIX, Intelligent Miner, WebSphere, Netfinity, Tivoli and Informix aretrademarks or registered trademarks of IBM Corporation.
Linux is the registered trademark of Linus Torvalds in the U.S. and other countries.
Adobe, the Adobe logo, Acrobat, PostScript, and Reader are either trademarks or registered trademarks of Adobe SystemsIncorporated in the United States and/or other countries.
Oracle is a registered trademark of Oracle Corporation.
UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open Group.
Citrix, ICA, Program Neighborhood, MetaFrame, WinFrame, VideoFrame, and MultiWin are trademarks or registeredtrademarks of Citrix Systems, Inc.
HTML, XML, XHTML and W3C are trademarks or registered trademarks of W3C, World Wide Web Consortium,
Massachusetts Institute of Technology.
Java is a registered trademark of Sun Microsystems, Inc.
JavaScript is a registered trademark of Sun Microsystems, Inc., used under license for technology invented andimplemented by Netscape.
SAP, R/3, SAP NetWeaver, Duet, PartnerEdge, ByDesign, SAP BusinessObjects Explorer, StreamWork, and other SAPproducts and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAPAG in Germany and other countries.
Business Objects and the Business Objects logo, BusinessObjects, Crystal Reports, Crystal Decisions, Web Intelligence,Xcelsius, and other Business Objects products and services mentioned herein as well as their respective logos aretrademarks or registered trademarks of Business Objects Software Ltd. Business Objects is an SAP company.
Sybase and Adaptive Server, iAnywhere, Sybase 365, SQL Anywhere, and other Sybase products and services mentionedherein as well as their respective logos are trademarks or registered trademarks of Sybase, Inc. Sybase is an SAPcompany.
All other product and service names mentioned are the trademarks of their respective companies. Data contained in thisdocument serves informational purposes only. National product specifications may vary.
These materials are subject to change without notice. These materials are provided by SAP AG and its affiliated companies("SAP Group") for informational purposes only, without representation or warranty of any kind, and SAP Group shall not beliable for errors or omissions with respect to the materials. The only warranties for SAP Group products and services arethose that are set forth in the express warranty statements accompanying such products and services, if any. Nothingherein should be construed as constituting an additional warranty.
7/31/2019 Android+ +How+to+Guide
3/31
Copyright/Trademark
Table of Contents
Consuming SAP NetWeaver Gateway Services from Android Applications............................. 4
Prerequisites ............................................................................................................................................. 5
Building an Android Application ............................................................................................................. 6
Function Import Example .................................................................................................................... 6
Read Example .................................................................................................................................... 10
Create Example .................................................................................................................................. 12
Creation of Request Body for Create Method................................................................................ 14
Authentication Using X.509 Certificate ............................................................................................... 15
Prerequisites ....................................................................................................................................... 15
Android Project Configuration .......................................................................................................... 16
Read Example using X.509 .............................................................................................................. 16
Appendix 1 - RMTSAMPLEFLIGHT Service Metadata Description............................................ 18
Appendix 2
Code Snippets .......................................................................................................... 24
Read - Get Carrier by Carrier ID .......................................................................................................... 24
Function Import....................................................................................................................................... 26
Create - Booking a Flight ...................................................................................................................... 28
Appendix 3 Code Snippets using x.509..................................................................................... 30
7/31/2019 Android+ +How+to+Guide
4/31
Copyright/Trademark
CONSUMING SAP NETWEAVER GATEWAY SERVICES
FROM ANDROID APPLICATIONS
SAP NetWeaver Gateway technology provides a simple way to interact with SAP applications through
variety of devices, environments and platforms based on market standards. The framework enables
development of innovative, people-centric solutions that bring the power of SAP business software into
new experiences, such as: social networking and collaboration environments; mobile and tablet devices;
and rich internet applications. The framework supports rapid innovation while ensuring security, integrity,
management and optimized maintenance of the core SAP systems. Completely flexible, the software offers
connectivity to SAP applications using any programming language or model without the need for SAP
knowledge by taking advantage of REST services and OData/ATOM protocols.
This guide provides information on how to consume SAP NetWeaver Gateway services from Android
applications based on the following scenario:
The user wants to fly from New York to San Francisco.
He goes online and searches for the different flights available. He enters his search parameters: city of
origin, destination city, departure date, and arrival date (Function Import).
From the SAP NetWeaver Gateway, he receives the information for the option(s) that meet(s) his search
parameters (Get Function).
He can now select the one that suits him the most and book his flight (Create Function).
The links to access the service document and metadata document are provided the in table below.
Service
Document
http://:/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/
Metadata
Document
http://:< Gateway_port>/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/
$metadata?$format=xml
Each service URL should contain the following information:
Gateway host
Port
SAP Client no need to specify client if connecting to the default client.
Example:
http://:/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT?sap-client=100
To obtain information on the selected service (its properties, function imports, associations, etc.), refer to
Appendix 1, were we show the Metadata description of the RMTSAMPLEFLIGHT service.
To view the code snippets in a format that can be copied into new code, refer toAppendix 2 Code
Snippets.
7/31/2019 Android+ +How+to+Guide
5/31
Copyright/Trademark
The Flight service contains the following collections:
Collection URL
FlightCollection (list of flights) http://:/sap/opu/sdata/
iwfnd/RMTSAMPLEFLIGHT/FlightCollection
CarrierCollection (list of carriers http://:/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/CarrierCollection
BookingsCollection (list of Bookings) http://:/sap/opu/sdata/
iwfnd/RMTSAMPLEFLIGHT/BookingCollection
The operation GetAvailableFlights on FlightCollection is used to get a list of flights. The following is the URL
for the same:
http://:/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/GetAvailableFlights?Each collection supports some or all of the operations listed below. The table below provides the list of
operations supported and the methods that each of this operations map to respectively:
Operation Method
Create HTTP POST
Read GET
Update PUT
Delete DELETE
Prerequisites
The following tools are required for developing an Android application:
Software Version
SAP NetWeaver Gateway 2.0
Eclipse 3.6
OS Android 2.1
Contact your system administrator for landscape details to connect to the SAP NetWeaver Gateway system.
7/31/2019 Android+ +How+to+Guide
6/31
Copyright/Trademark
Building an Android Application
Function Import Example
To get a list of the available flights, based on filter criteria, triggerthe GetAvailableFlights function import
with cityFrom, cityTo, fromDate and toDate parameters. The response will contain a list of flights that meet
the criteria.
Note: For more information on Function Import (also called Service Operation), refer to OData
documentation at http://www.odata.org/developers/protocols/uri-conventionsunder the Addressing
Service Operations section.
Refer to the code snippet below for details on how to do it from an Android application:
1. Use the following method for executing a Function Import that returns a collection of flights.
/**
*
* @param cityFrom
* @param cityTo
* @param date* @return available flights
* @throws URISyntaxException
* @throws ClientProtocolException
* @throws IOException
*/
public String getFlightList(String cityFrom, String cityTo, String fromDate, StringtoDate) throws URISyntaxException, ClientProtocolException, IOException
{
2. Create the request URL that will be sent.
String requestUrl =
http://:/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/GetAvailableFligh
ts?cityfrom= + cityFrom + "&cityto=" + cityTo + "&fromdate=" + fromDate + "&todate=" +toDate;
3. Define HTTP client.
DefaultHttpClient client = new DefaultHttpClient();client.getCredentialsProvider().setCredentials(AuthScope. ANY,
new UsernamePasswordCredentials("", ""));
4. Create a Get request.
HttpGet getRequest = new HttpGet();
// buffer reader to read the response
BufferedReader bufferedReader =null
;
// placeholder for the service response
HttpResponse response = null;
getRequest.setURI(new URI(requestUrl));getRequest.setHeader("Content-Type", "text/xml");
getRequest.setHeader("Host", getRequest.getURI().getHost());
5. Execute the request.
response = client.execute(getRequest);
7/31/2019 Android+ +How+to+Guide
7/31
Copyright/Trademark
bufferedReader = new BufferedReader(new
InputStreamReader(response.getEntity().getContent()));
StringBuffer stringBuffer = new StringBuffer("");
String line = "";
try{
while ((line = bufferedReader.readLine()) != null)
{stringBuffer.append(line);
}
bufferedReader.close();
}
catch (IOException e)
{
Log.e("IO exception", e.toString());}
// XML response, needs to be parsed as described below.
return stringBuffer.toString();
}
To parse the properties for each entry from the feed follow the example below:
publicvoid parsePropertiesFromFeed(String xml)
{DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
String propertyValue = null;
String propertyName = null;
try
{DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(new ByteArrayInputStream(xml.getBytes()));
Element root = dom.getDocumentElement();
NodeList entries = root.getElementsByTagName("atom:entry");
// iterate over entries
for (int i = 0; i < entries.getLength(); i++)
{
Toast.makeText(getApplicationContext(), "entry number: " + i,
Toast.LENGTH_SHORT).show();
Node atomContentNode = entries.item(i).getChildNodes().item(1);If (atomContentNode.getNodeName().equalsIgnoreCase( "atom:content"))
{
Node mpropertiesNode = atomContentNode.getChildNodes().item(0);
if (mpropertiesNode.getNodeName().equalsIgnoreCase( "m:properties"))
{NodeList mpropertiesList = mpropertiesNode.getChildNodes();
// iterate over entry propertiesfor (int j = 0; j < mpropertiesList.getLength(); j++)
{
propertyName = mpropertiesList.item(j).getNodeName();
propertyValue =
mpropertiesList.item(j).getFirstChild().getNodeValue(
);
Toast.makeText(getApplicationContext(), propertyName
+ ": " + propertyValue, Toast.LENGTH_SHORT).show();
}
}
7/31/2019 Android+ +How+to+Guide
8/31
Copyright/Trademark
}
}
}
catch (Exception e)
{
thrownew RuntimeException(e.getMessage());}
}
Response
-http://:/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/FlightCollectionFlightCollection2011-09-27T06:32:42Z
-
-
-AA00172011-01-05T00:00:00889.00USD747-400385367185742.7331222121
-USnew yorkJFKUS
SAN FRANCISCOSFO361PT11H00M00SPT14H01M00S2574.0000SMI0
http://:/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/FlightCollection(carrid='AA',connid='0017',fldate='20110105')
Flight
http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/GetAvailableFlights?cityfrom=NEW%20YORK&cityto=SAN%20FRANCISCO&fromdate=20110105&todate=20110728http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/GetAvailableFlights?cityfrom=NEW%20YORK&cityto=SAN%20FRANCISCO&fromdate=20110105&todate=20110728http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/GetAvailableFlights?cityfrom=NEW%20YORK&cityto=SAN%20FRANCISCO&fromdate=20110105&todate=20110728http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/GetAvailableFlights?cityfrom=NEW%20YORK&cityto=SAN%20FRANCISCO&fromdate=20110105&todate=20110728http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/GetAvailableFlights?cityfrom=NEW%20YORK&cityto=SAN%20FRANCISCO&fromdate=20110105&todate=20110728http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/GetAvailableFlights?cityfrom=NEW%20YORK&cityto=SAN%20FRANCISCO&fromdate=20110105&todate=20110728http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/GetAvailableFlights?cityfrom=NEW%20YORK&cityto=SAN%20FRANCISCO&fromdate=20110105&todate=20110728http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/GetAvailableFlights?cityfrom=NEW%20YORK&cityto=SAN%20FRANCISCO&fromdate=20110105&todate=20110728http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/GetAvailableFlights?cityfrom=NEW%20YORK&cityto=SAN%20FRANCISCO&fromdate=20110105&todate=20110728http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/GetAvailableFlights?cityfrom=NEW%20YORK&cityto=SAN%20FRANCISCO&fromdate=20110105&todate=20110728http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/GetAvailableFlights?cityfrom=NEW%20YORK&cityto=SAN%20FRANCISCO&fromdate=20110105&todate=20110728http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/GetAvailableFlights?cityfrom=NEW%20YORK&cityto=SAN%20FRANCISCO&fromdate=20110105&todate=20110728http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/GetAvailableFlights?cityfrom=NEW%20YORK&cityto=SAN%20FRANCISCO&fromdate=20110105&todate=20110728http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/GetAvailableFlights?cityfrom=NEW%20YORK&cityto=SAN%20FRANCISCO&fromdate=20110105&todate=20110728http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/GetAvailableFlights?cityfrom=NEW%20YORK&cityto=SAN%20FRANCISCO&fromdate=20110105&todate=201107287/31/2019 Android+ +How+to+Guide
9/31
Copyright/Trademark
2011-09-27T06:32:42Z
--
-AA
00172011-03-16T00:00:00
422.94USD747-400385371192129.22313021
20-
USnew yorkJFKUSSAN FRANCISCO
SFO361PT11H00M00SPT14H01M00S2574.0000SMI0
http://:/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/FlightCollection(carrid='AA',conn
id='0017',fldate='20110316')
Flight2011-09-27T06:32:42Z
http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/GetAvailableFlights?cityfrom=NEW%20YORK&cityto=SAN%20FRANCISCO&fromdate=20110105&todate=20110728http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/GetAvailableFlights?cityfrom=NEW%20YORK&cityto=SAN%20FRANCISCO&fromdate=20110105&todate=20110728http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/GetAvailableFlights?cityfrom=NEW%20YORK&cityto=SAN%20FRANCISCO&fromdate=20110105&todate=20110728http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/GetAvailableFlights?cityfrom=NEW%20YORK&cityto=SAN%20FRANCISCO&fromdate=20110105&todate=20110728http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/GetAvailableFlights?cityfrom=NEW%20YORK&cityto=SAN%20FRANCISCO&fromdate=20110105&todate=20110728http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/GetAvailableFlights?cityfrom=NEW%20YORK&cityto=SAN%20FRANCISCO&fromdate=20110105&todate=20110728http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/GetAvailableFlights?cityfrom=NEW%20YORK&cityto=SAN%20FRANCISCO&fromdate=20110105&todate=20110728http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/GetAvailableFlights?cityfrom=NEW%20YORK&cityto=SAN%20FRANCISCO&fromdate=20110105&todate=20110728http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/GetAvailableFlights?cityfrom=NEW%20YORK&cityto=SAN%20FRANCISCO&fromdate=20110105&todate=20110728http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/GetAvailableFlights?cityfrom=NEW%20YORK&cityto=SAN%20FRANCISCO&fromdate=20110105&todate=20110728http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/GetAvailableFlights?cityfrom=NEW%20YORK&cityto=SAN%20FRANCISCO&fromdate=20110105&todate=20110728http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/GetAvailableFlights?cityfrom=NEW%20YORK&cityto=SAN%20FRANCISCO&fromdate=20110105&todate=201107287/31/2019 Android+ +How+to+Guide
10/31
Copyright/Trademark
Read Example
To read the carrier details, you must call the CarrierCollection using the Carrier ID parameter.
Refer to the code snippet below for details on how to do it from an Android application:
1. Create a Get Specific carrier query using the method below, the result returned in the response is a
Carrier XML entry.
/**
* Method for getting Carrier information by its carrierId
* @param carrid the carrier id to search for, e.g. LH* @return Carrier xml entry
* @throws URISyntaxException
* @throws ClientProtocolException
* @throws IOException
*/
public String getCarrier(String carrid) throws URISyntaxException, ClientProtocolException,
IOException
{
2. Create the request URL that will be sent.
String requestUrl =http://:/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/CarrierCollection
(carrid=' + carrid + "')";
3. Define HTTP client.
DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(AuthScope. ANY,
new UsernamePasswordCredentials("", ""));
4. Create a Get request.
HttpGet getRequest = new HttpGet();
// buffer reader to read the responseBufferedReader bufferedReader = null;
// placeholder for the service response
HttpResponse response = null;
getRequest.setURI(new URI(requestUrl));
getRequest.setHeader("Content-Type", "text/xml");
getRequest.setHeader("Host", getRequest.getURI().getHost());
5. Execute the request.
response = client.execute(getRequest);
bufferedReader = new BufferedReader(new
InputStreamReader(response.getEntity().getContent()));
StringBuffer stringBuffer = new StringBuffer("");
String line = "";
try
{
while ((line = bufferedReader.readLine()) != null)
{stringBuffer.append(line);
}
bufferedReader.close();
}
7/31/2019 Android+ +How+to+Guide
11/31
Copyright/Trademark
catch (IOException e)
{
Log.e("IO exception", e.toString());
}
// XML response, needs to be parsed as described below.
return stringBuffer.toString();}
To parse the properties from the entry xml, follow the example below:
publicvoid parsePropertiesFromEntry(String xml)
{DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
String propertyValue = null;
String propertyName = null;
try
{
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(new ByteArrayInputStream(xml.getBytes()));
Element root = dom.getDocumentElement();
NodeList properties =root.getElementsByTagName( "m:properties").item(0).getChildNodes();// iterate over the entry properties
for (int i = 0; i < properties.getLength(); i++)
{
propertyName = properties.item(i).getNodeName();
propertyValue = properties.item(i).getFirstChild().getNodeValue();
Toast.makeText(getApplicationContext(), propertyName + ": " +
propertyValue, Toast.LENGTH_SHORT).show();
}
}catch (Exception e)
{thrownew RuntimeException(e.getMessage());
}
}
Response
-
-
AAAmerican AirlinesUSDhttp://www.aa.com
:/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/CarrierCollection(carrid='AA')
Carrier2011-09-27T10:00:38Z
http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/CarrierCollection(carrid='AA')http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/CarrierCollection(carrid='AA')http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/CarrierCollection(carrid='AA')http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/CarrierCollection(carrid='AA')http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/CarrierCollection(carrid='AA')http://vmw3815.wdf.sap.corp:50009/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/CarrierCollection(carrid='AA')7/31/2019 Android+ +How+to+Guide
12/31
Copyright/Trademark
Create Example
To book a flight, trigger the HTTP POST method on the Booking Collection (with the Booking details in the
request body). Check for the HTTP response code. If the response code is 201, then the HTTP-POST is
successful. The response contains the newly created Booking ID in the bookidfield, as well as the entireinformation that was part of the request body.
Refer to the code snippet below for details on how to invoke a create request from an Android application:
1. Prepare a new Booking entry by creating an entry payload.
String content = "" +
"" +
"AA" +
"0017" +
"2011-07-27T00:00:00" +
"" +"00004617" +
"P" +
"" +
"KG" +
"14.4000" +
"Y" +
"879.82" +"USD" +
"803.58" +
"USD" +
"2011-05-22T00:00:00" +"00000000" +
"00000325" +
"" +
"Joe Smith" +
"1234567" +"1990-10-10" +
"";
/**
7/31/2019 Android+ +How+to+Guide
13/31
Copyright/Trademark
2. Use the Method below to book a new flight.
*
* @param content
* @return
* @throws IOException* @throws ClientProtocolException
*/
publicString createBooking(String content) throws ClientProtocolException, IOException{
DefaultHttpClient client;client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(AuthScope. ANY,
new UsernamePasswordCredentials("", ""));
HttpPost post = new HttpPost(
"http://:/sap/opu/sdata/IWFND/RMTSAMPLEFLIGHT/BookingCollection")
;
3. Add the following header to the Post requests:
post.addHeader("X-Requested-With", "XMLHttpRequest");
4. Add the request body.
StringEntity entity = new StringEntity(content);
entity.setContentType(new BasicHeader("Content-Type", "application/atom+xml"));post.setEntity(entity);
5. Execute the request.
HttpResponse response;
response = client.execute(post);
if (response.getStatusLine().getStatusCode() != 201){
thrownew RuntimeException(response.getStatusLine().getReasonPhrase());
}
InputStream inputStream = response.getEntity().getContent();
// convert to String and return the response
return convertInputStreamToString(inputStream);
}
7/31/2019 Android+ +How+to+Guide
14/31
Copyright/Trademark
Creation of Request Body for Create Method
The request body can be easily created by looking at the tags in the collections metadata. For example, the
metadata for Booking Collection provides information about the various fields (tags) that are needed for
creating a booking:
7/31/2019 Android+ +How+to+Guide
15/31
7/31/2019 Android+ +How+to+Guide
16/31
Copyright/Trademark
Android Project Configuration
To allow the code to use a CA Certificate and an X.509 Client Certificate:
1. Using the Portecle tool, create a trust store and a key store file. The key store will contain your X.509
Client Certificate and the trust store will contain the CA Certificate. Both files must be of type BKS when
created using the Portecle tool.Note: For instructions on how to create a key store/trust store, refer to the Portecle documentation at
http://portecle.sourceforge.net/create-keystore.html.
2. Import a CA Certificate to the trust store and an X.509 Client Certificate to the key store.
For instructions on how to import a CA Certificate to a trust store, refer to the Portecle
documentation at http://portecle.sourceforge.net/import-trusted-cert.html.
For instructions on how to import an X.509 Client Certificate to a key store, refer to the Portecle
documentation at http://portecle.sourceforge.net/import-keypair.html.
3. Add the key store and the trust store to your Android project under /res/raw/.
Read Example using X.509
Refer to the code snippet below for details on how to send a request to a Java Android application using
X.509 Certificate Authentication:
1. Load the truststore certificate.
InputStream clientTruststoreIs = getResources().openRawResource(R.raw.truststore);KeyStore trustStore = null;
trustStore = KeyStore.getInstance("BKS");
trustStore.load(clientTruststoreIs, password.toCharArray());
System.out.println("Loaded server certificates: " + trustStore.size());
2. Initialize the trust manager factory using the read truststore.
TrustManagerFactory trustManagerFactory = null;
trustManagerFactory =TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore);
3. Load the client certificate.
InputStream keyStoreStream = getResources().openRawResource(R.raw.keystore);
KeyStore keyStore = null;keyStore = KeyStore.getInstance("BKS");
keyStore.load(keyStoreStream, password.toCharArray());
System.out.println("Loaded client certificates: " + keyStore.size());
4. Initialize the key manager factory with the read client certificate.
KeyManagerFactory keyManagerFactory = null;
keyManagerFactory =
KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());keyManagerFactory.init(keyStore,password.toCharArray());
http://portecle.sourceforge.net/create-keystore.htmlhttp://portecle.sourceforge.net/create-keystore.htmlhttp://portecle.sourceforge.net/import-trusted-cert.htmlhttp://portecle.sourceforge.net/import-keypair.htmlhttp://portecle.sourceforge.net/import-keypair.htmlhttp://portecle.sourceforge.net/import-trusted-cert.htmlhttp://portecle.sourceforge.net/create-keystore.html7/31/2019 Android+ +How+to+Guide
17/31
Copyright/Trademark
5. Initialize the SSLSocketFactory to use the certificates.
SSLSocketFactory socketFactory = null;
socketFactory = new SSLSocketFactory(SSLSocketFactory.TLS, keyStore, password,trustStore, null, null);
6. Set the basic data.
HttpParams params = new BasicHttpParams();HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");HttpProtocolParams.setUseExpectContinue(params, true);
HttpProtocolParams.setUserAgent(params, "Android app/1.0.0");
7. Initialize the ConnPerRouteBeanparameters.
ConnPerRoute connPerRoute = new ConnPerRouteBean(12);
ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);
ConnManagerParams.setMaxTotalConnections(params, 20);
8. Set timeout.
HttpConnectionParams.setStaleCheckingEnabled(params, false);
HttpConnectionParams.setConnectionTimeout(params, 20 * 1000);HttpConnectionParams.setSoTimeout(params, 20 * 1000);
HttpConnectionParams.setSocketBufferSize(params, 8192);
9. Initialize HTTP client parameters.
HttpClientParams.setRedirecting(params, false);
10.Register the schemes in the scheme registry.
SchemeRegistry schReg = new SchemeRegistry();
schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 50009));
schReg.register(new Scheme("https", socketFactory, 44309));
ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);
DefaultHttpClient sClient = new DefaultHttpClient(conMgr, params);
HttpGet httpGet = newHttpGet("https://vmw3815.wdf.sap.corp:44309/sap/opu/sdata/iwfnd/sflight/?sap-
client=100&$format=xml");
HttpResponse response = sClient.execute(httpGet);HttpEntity httpEntity = response.getEntity();
Note: The service response should be parsed according to the OData/SAP Data Protocols. This parsing is not
demonstrated in this code snippet, and should be implemented by the developer, if needed. For further
details, refer toAppendix 2 Code Snippets.
For the original X.509 code usage, refer toAppendix 3 Code Snippets using x.509.
7/31/2019 Android+ +How+to+Guide
18/31
Copyright/Trademark
Appendix 1 - RMTSAMPLEFLIGHT Service Metadata
Description
7/31/2019 Android+ +How+to+Guide
19/31
Copyright/Trademark
7/31/2019 Android+ +How+to+Guide
20/31
Copyright/Trademark
7/31/2019 Android+ +How+to+Guide
21/31
Copyright/Trademark
7/31/2019 Android+ +How+to+Guide
22/31
Copyright/Trademark
Airline
Flight Number
Date
7/31/2019 Android+ +How+to+Guide
23/31
Copyright/Trademark
Date
Date
Depart.city
Arrival city
7/31/2019 Android+ +How+to+Guide
24/31
Copyright/Trademark
APPENDIX 2 CODE SNIPPETS
Read - Get Carrier by Carrier ID
Implement the Java Doc.
/*** Method for getting Carrier information by its carrierId
* @param carrid the carrier id to search for, e.g. LH
* @return Carrier xml entry* @throws URISyntaxException
* @throws ClientProtocolException* @throws IOException
*/
public String getCarrier(String carrid) throws URISyntaxException,
ClientProtocolException, IOException
{// create the request URL
String requestUrl =
http://:/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/CarrierCollection(carrid=' + carrid + "')";
// http get client
DefaultHttpClient client = new DefaultHttpClient();client.getCredentialsProvider().setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("", ""));HttpGet getRequest = new HttpGet();
// buffer reader to read the response
BufferedReader bufferedReader = null;
// the service response
HttpResponse response = null;// construct a URI objectgetRequest.setURI(new URI(requestUrl));
getRequest.setHeader("Content-Type", "text/xml");
getRequest.setHeader("Host", getRequest.getURI().getHost());
// execute the request
response = client.execute(getRequest);
bufferedReader = new BufferedReader(new
InputStreamReader(response.getEntity().getContent()));
StringBuffer stringBuffer = new StringBuffer("");String line = "";
try
{
while ((line = bufferedReader.readLine()) != null)
{stringBuffer.append(line);
}
bufferedReader.close();}
catch (IOException e)
7/31/2019 Android+ +How+to+Guide
25/31
Copyright/Trademark
{
Log.e("IO exception", e.toString());
}
// response, needs to be parsedreturnstringBuffer.toString();
}
// example on how to parse the properties from the entry xml
publicvoid parsePropertiesFromEntry(String xml)
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
String propertyValue = null;
String propertyName = null;
try
{
DocumentBuilder builder = factory.newDocumentBuilder();Document dom = builder.parse(new ByteArrayInputStream(xml.getBytes()));
Element root = dom.getDocumentElement();NodeList properties =
root.getElementsByTagName("m:properties").item(0).getChildNodes();// iterate over the entry properties
for (int i = 0; i < properties.getLength(); i++)
{
propertyName = properties.item(i).getNodeName();
propertyValue = properties.item(i).getFirstChild().getNodeValue();
Toast.makeText(getApplicationContext(), propertyName + ": " +
propertyValue, Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
thrownew RuntimeException(e.getMessage());}
}
7/31/2019 Android+ +How+to+Guide
26/31
Copyright/Trademark
Function Import
/**
* Method for executing a Function Import that returns a collection of* flights.
* @param from
* @param to* @param date
* @return available flights* @throws URISyntaxException
* @throws ClientProtocolException* @throws IOException
*/public String getFlightList(String cityFrom, String cityTo, String date) throws
URISyntaxException, ClientProtocolException, IOException
{
// create the request URLString requestUrl =
http://:/sap/opu/sdata/iwfnd/RMTSAMPLEFLIGHT/GetAvailableFlights?cityfrom= + cityFrom + "&cityto=" + cityTo + "&fromdate=" + date +
"&todate=" + date;
// http get client
DefaultHttpClient client = new DefaultHttpClient();client.getCredentialsProvider().setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("", ""));
HttpGet getRequest = new HttpGet();
// buffer reader to read the responseBufferedReader bufferedReader = null;
// the service responseHttpResponse response = null;
// construct a URI object
getRequest.setURI(new URI(requestUrl));
getRequest.setHeader("Content-Type", "text/xml");getRequest.setHeader("Host", getRequest.getURI().getHost());
// execute the request
response = client.execute(getRequest);
bufferedReader = new BufferedReader(new
InputStreamReader(response.getEntity().getContent()));
StringBuffer stringBuffer = new StringBuffer("");String line = "";
try
{
while ((line = bufferedReader.readLine()) != null)
{stringBuffer.append(line);
}
bufferedReader.close();
}
7/31/2019 Android+ +How+to+Guide
27/31
Copyright/Trademark
catch (IOException e)
{
Log.e("IO exception", e.toString());}
// response, needs to be parsedreturn stringBuffer.toString();
}
// an example of how to parse the properties for each entry from the feed // xml
publicvoid parsePropertiesFromFeed(String xml)
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
String propertyValue = null;
String propertyName = null;
try
{
DocumentBuilder builder = factory.newDocumentBuilder();Document dom = builder.parse(new ByteArrayInputStream(xml.getBytes()));
Element root = dom.getDocumentElement();NodeList entries = root.getElementsByTagName("atom:entry");
// iterate over entries
for (int i = 0; i < entries.getLength(); i++)
{
Toast.makeText(getApplicationContext(), "entry number: " + i,
Toast.LENGTH_SHORT).show();Node atomContentNode = entries.item(i).getChildNodes().item(1);
If (atomContentNode.getNodeName().equalsIgnoreCase("atom:content")){
Node mpropertiesNode =
atomContentNode.getChildNodes().item(0);
if(mpropertiesNode.getNodeName().equalsIgnoreCase("m:properties"))
{NodeList mpropertiesList =
mpropertiesNode.getChildNodes();
// iterate over entry properties
for (int j = 0; j < mpropertiesList.getLength(); j++){
propertyName =mpropertiesList.item(j).getNodeName();
propertyValue =mpropertiesList.item(j).getFirstChild().getNodeV
alue();
Toast.makeText(getApplicationContext(),propertyName + ": " + propertyValue,
Toast.LENGTH_SHORT).show();
}}
}}
}
7/31/2019 Android+ +How+to+Guide
28/31
Copyright/Trademark
catch (Exception e)
{
thrownew RuntimeException(e.getMessage());}
}
Create - Booking a Flight
// create the entry payload
String content = "" +
"" +"AA" +
"0017" +"2011-07-27T00:00:00" +
"" +"00004617" +
"P" +
"" +"KG" +
"14.4000" +
"Y" +
"879.82" +"USD" +
"803.58" +"USD" +"2011-05-22T00:00:00" +
"00000000" +"00000325" +
"" +"Joe Smith" +
"1234567" +
"1990-10-10" +
"";
/**
* Method for creating a new flight booking.*
* @param content
* @return* @throws IOException
* @throws ClientProtocolException*/
publicString createBooking(String content) throws ClientProtocolException, IOException
{DefaultHttpClient client;
client = new DefaultHttpClient();
7/31/2019 Android+ +How+to+Guide
29/31
Copyright/Trademark
client.getCredentialsProvider().setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("", ""));
HttpPost post = new HttpPost("http://:/sap/opu/sdata/IWFND/RMTSAMPLEFLIGHT/Booking
Collection");
// you must add this header to post requests
post.addHeader("X-Requested-With", "XMLHttpRequest");
HttpResponse response;StringEntity entity = new StringEntity(content);
entity.setContentType(new BasicHeader("Content-Type", "application/atom+xml"));post.setEntity(entity);
response = client.execute(post);
if (response.getStatusLine().getStatusCode() != 201)
{thrownew RuntimeException(response.getStatusLine().getReasonPhrase());
}
InputStream inputStream = response.getEntity().getContent();
// convert to String and return the response
returnconvertInputStreamToString(inputStream);}
7/31/2019 Android+ +How+to+Guide
30/31
Copyright/Trademark
APPENDIX 3 CODE SNIPPETS USING X.509
final String password = "mysecret";
/** Called when the activity is first created. */publicvoid onCreate(Bundle savedInstanceState)
{super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
// setup truststore to provide trust for the server certificate
// load truststore certificateInputStream clientTruststoreIs = getResources().openRawResource(R.raw.truststore);
KeyStore trustStore = null;
trustStore = KeyStore.getInstance("BKS");
trustStore.load(clientTruststoreIs, password.toCharArray());
System.out.println("Loaded server certificates: " + trustStore.size());
// initialize trust manager factory with the read truststore
TrustManagerFactory trustManagerFactory = null;
trustManagerFactory =TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore);
// setup client certificate
// load client certificate
InputStream keyStoreStream = getResources().openRawResource(R.raw.keystore);KeyStore keyStore = null;
keyStore = KeyStore.getInstance("BKS");keyStore.load(keyStoreStream, password.toCharArray());
System.out.println("Loaded client certificates: " + keyStore.size());
// initialize key manager factory with the read client certificateKeyManagerFactory keyManagerFactory = null;
keyManagerFactory =
KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore,password.toCharArray());
// initialize SSLSocketFactory to use the certificatesSSLSocketFactory socketFactory = null;
socketFactory = new SSLSocketFactory(SSLSocketFactory.TLS, keyStore, password,trustStore, null, null);
// Set basic dataHttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpProtocolParams.setUseExpectContinue(params, true);
7/31/2019 Android+ +How+to+Guide
31/31
Copyright/Trademark
HttpProtocolParams.setUserAgent(params, "Android app/1.0.0");
// Make poolConnPerRoute connPerRoute = new ConnPerRouteBean(12);
ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);ConnManagerParams.setMaxTotalConnections(params, 20);
// Set timeoutHttpConnectionParams.setStaleCheckingEnabled(params, false);
HttpConnectionParams.setConnectionTimeout(params, 20 * 1000);HttpConnectionParams.setSoTimeout(params, 20 * 1000);
HttpConnectionParams.setSocketBufferSize(params, 8192);
// Some client paramsHttpClientParams.setRedirecting(params, false);
// Register http/s shemas!SchemeRegistry schReg = new SchemeRegistry();
schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 50009));schReg.register(new Scheme("https", socketFactory, 44309));
ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);DefaultHttpClient sClient = new DefaultHttpClient(conMgr, params);
HttpGet httpGet = newHttpGet("https://vmw3815.wdf.sap.corp:44309/sap/opu/sdata/iwfnd/sflight/?sap-
client=100&$format=xml");HttpResponse response = sClient.execute(httpGet);
HttpEntity httpEntity = response.getEntity();
InputStream is = httpEntity.getContent();
BufferedReader read = new BufferedReader(new InputStreamReader(is));String query = null;
while ((query = read.readLine()) != null)System.out.println(query);
} catch (Exception e) {
e.printStackTrace();
}