Obtaining data page contents and metadata with the Connect SDK on Android
This tutorial demonstrates how to configure and integrate Connect SDK features with a new Android application created in the Android Studio development environment by using Java language. Its purpose is to show how to use the Connect SDK Data API to obtain the contents of a data page and also to obtain the metadata for a specific data page.
DataPageTab
and DataPageActivity
demo application classes that are part of the com.pega.helloconnectsdk
package.Prerequisites
Before you start this tutorial, do the following tasks:
- Read Creating a Hello World Android app with the Connect SDK, which describes how to set up the Connect SDK in the Android Studio environment, initialize it, and use it to authenticate.
- Download ConnectSDKDemo.zip which contains the sample Java code and other Android Studio project files for the Android app described in this tutorial.
You can connect to any Pega® Platform instance to test the Android app. Make sure that it already includes some cases and assignments.
Obtaining data page contents
To add logic that allows your native Android app to obtain the data page contents from a Pega instance, you must call the getDataPage()
method for the already initialized Connect SDK and pass it the name of the data page. The two Java classes created in this section are reused to also obtain the data page metadata, as explained in the next section.
- In the Android Studio environment, right-click the package where the new class is to be created, and click
Fragment
. You must use a specialTabHost
that allows the use ofFragment
objects for its tab content. For more information, consult theTabsActivity.java
file for the included sample project and the Android developer guide. . Proceed to create a subclass of - Name the class
DataPageTab
. This subclass is where all the logic that is related to obtaining the data page contents for a Pega Platform application is placed. It is also used to obtain the metadata page contents. This is explained in the next section. - Override its
onCreateView()
callback method so that it includes a reference to the button inside it. - Within the method, create an anonymous instance of the
View.OnClickListener()
class and, inside of its overridenonClick()
method, add Java code that advances to the next screen. To do so, an instance of theDataPageActivity
object is created and started in the Android app, passing it the name of the data page for which to display the contents. This object is created in the next step. - In the Android Studio environment, right-click the package where the new class is to be created, and click
Activity
. Name the subclassDataPageActivity
. This class is where all the logic that is related to displaying data page and metadata contents is placed. . Create a subclass of - Override the
onCreate()
callback method, which is called when an instance of this new class is called. - Obtain the data page name that was passed to this class and use it to obtain its contents. Notice that within the
else if()
statement, thedata().getDataPage()
Connect SDK method is called on the previously obtainedPega
object. You obtain this object by first getting an instance of thePegaSingleton
object by using thegetInstance()
method, and then calling thegetPega()
method on it. Notice that you pass the current name of the data page to thegetDataPage()
method as its first parameter. If the data page content was successfully obtained, within theonSuccess()
callback you display it on the screen. Otherwise, if an error occurred, within theonFailure()
callback you log an error message.
Get data/Get metadata - Data Page API
Obtaining data page metadata
To get the metadata of a data page in your Pega Platform application, you must call the getDataPageMetadata()
method for the already initialized Connect SDK by passing it the name of the data page. The DataPageTab
and DataPageActivity
classes created in the previous section is used to add this functionality.
- In the
DataPageTab.java
file, edit the contents within theonCreateView()
callback method so that it also includes a reference to the button inside it. - Within this callback method, create an anonymous instance of the
View.OnClickListener()
class and, inside of its overridenonClick()
method, add Java code that advances to the next screen. To do so, an instance of theDataPageActivity
object is created and started in the Android app, passing it the name of the data page for which to display the metadata. - In the
DataPageActivity.java
file, add the code within theif()
statement that obtains the metadata content for the passed data page. This is where thedata().getDataPageMetadata()
Connect SDK method is called on the previously obtainedPega
object. You obtain this object by first getting an instance of thePegaSingleton
object by using thegetInstance()
method, and then calling thegetPega()
method on it. Notice that you pass the data page name as its first parameter. If the metadata was successfully obtained, within theonSuccess()
callback you display it on the screen. Otherwise, if an error occurred, within theonFailure()
callback you log an error message.