This blog series makes use of the
SAP Cloud Platform SDK for Android. Specifically the app created in this blog series will use the
Kotlin language which is the
now recommended language to build Android apps. The application will onboard with Mobile Services, make a proxied OData request, and in
part two, use
SAP Fiori Mentor to explore and use some of the available
UI controls that are included in the SAP Cloud Platform SDK for Android.
There are different approaches to creating an application with the SAP Cloud Platform SDK for Android.
The
Step by Step blog series shows how to use many of the SDK features with a from scratch approach.
The mission
Get Started with SAP Cloud Platform SDK for Android explores and customizes the starter or template application that can be created with the integrated SAP wizard.
The mission
Use Flows with SAP Cloud Platform SDK for Android demonstrates how the Flows library can be used to more easily onboard the application and make use of the provided onboarding screens.
This blog will make use of the SAP wizard to generate a Kotlin project without a UI. The UI will be created using sample code from the SAP Fiori Mentor.
Before proceeding, ensure that you have an Android development environment, the SAP Cloud Platform SDK for Android installed, and a Mobile Services trial account. This is covered in more detail in the
Try Out SAP Cloud Platform SDK for Android Wizard tutorial.
Create the Project
The following steps demonstrate how to create a new Kotlin project that will onboard with Mobile Services and contain proxy classes that enable data to be accessed and updated.
- Create the project.
- Specify the Mobile Services server to be used.
Additional details on how to configure the Mobile Services server to connect to are available in the tutorial Try Out SAP Cloud Platform SDK for Android Wizard.
- On the Cloud Configuration page, choose Create and specify the values as shown below.
- On the OData Services tab, choose Add.
Select the sample OData service.
- On the project properties tab, ensure the target language is Kotlin.
- On the Project Features tab, ensure the checkbox to create a user interface is unchecked.
Run the Project
In this section, the previously generated project will be examined and run.
- Notice that the generated app contains Java proxy classes which provide methods for retrieving (or creating, deleting and updating) data such as an asynchronous method to request the supplier data.
- The code to onboard the application with Mobile Services and to display the initial activity is in Kotlin. This can be seen as the extension for the files is .kt rather than .java.
- Run the app. The welcome screen appears the first time the app is run.
Enter your Mobile Services credentials to complete the onboarding.
Once the onboarding completes, the main screen of the app is shown. This will be enhanced in part 2 of this blog with the help of SAP Fiori Mentor and Fiori for Android controls.
Retrieving Data
In this section, code will be added to retrieve the list of suppliers.
- Add the following function to the class SimpleActivity.kt.
fun getSuppliers() {
val sapServiceManager = (application as SAPWizardApplication).sapServiceManager
var espmContainer = sapServiceManager.eSPMContainer
// import com.sap.cloud.android.odata.espmcontainer.Supplier
val query = DataQuery().orderBy(Supplier.supplierName)
espmContainer?.getSuppliersAsync(query, { suppliers: List<Supplier> ->
for (supplier in suppliers) {
Log.d(this.javaClass.name, "${supplier.supplierName} from ${supplier.city}")
}
//showRecycler(suppliers);
}, { re: RuntimeException -> Log.d(this.toString(), "An error occurred when querying for suppliers: " + re.message) })
}
If there are compilation errors in the code, it is likely due to missing. This can be corrected by using Android Studio's quick fix (Alt + Enter on Windows or Option + Enter on a Mac) or the Android Studio setting Add unambiguous imports on the fly.
The following are a few things to notice if you are new to Kotlin.
Additional information on the above topics can be found at Learn Kotlin by Example.
- At the end of the onCreate function, add the below call.
getSuppliers()
- Run the project and examine the logcat. Notice that each supplier is listed.
In part 2, the SAP Fiori Mentor will be used to help with the code needed to display a progress indicator while the data is being retrieved and the returned suppliers will be shown using contact cells in a recycler view.
Next (Part 2 Fiori Mentor App)