How to use Android API to interact with Onesait Platform?
Description
The first release of the Android API for onesait Platform is available to mobile developers. This API empowers mobile developers with the platform capabilities by allowing them to insert data from their apps into the platform.
In this version we have implemented 3 operations over the platform, all of them using REST communication:
JOIN: This operation allows the device to get a session in the platform. This is needed in order to perform an insertion with the following operation. The output of this operation is a sessionKey.
INSERT: This operation allows for insertion in an Ontology. A sessionKey and a valid Ontology instance are needed to perform this operation.
LEAVE: This operation closes the current session.
Development
To be able to use the API, it is necessary to add these lines to the build.gradle file in Android Studio. First one inside the repositories section, and the latter inside dependencies
maven{ url "http://nexus.onesaitplatform.com/nexus/content/repositories/releases/"}compile 'com.minsait.onesait.platform:onesaitplatform-android-client:1.0.0'
After Gradle synchronization, you may use the RestClient class to communicate with the platform:
The constructor requires to have a valid Digital Client (clientPlatform) registered in the platform, and its associated token. The Digital Client is the client interface to the platform, serving as an entry point to the system and selected ontologies for allowed devices. To get to the Client page, just navigate to:
Inside the creation menu, you should give your client a name and add the ontologies you want to gain access to through the client. A token will be generated, and that is the token the API needs to get access to the platform:
If you want to insert data into an Ontology, you will need to create one and associate it to your recently created IoTClient.
Once the platform elements are ready, you can go to Android Studio and start developing your app. First, you will need to instantiate the needed parameters to create your RestClient instance:
And then you may instantiate the RestClient object:
Once this is performed, it is pretty straightforward to implement any of the 3 operations defined. For instance, with this simple piece of code, you will be performing a JOIN and an INSERT in just 3 lines:
To insert, a compliant ontology instance needs to be provided as a String. Most of the common JSON frameworks like GSON and Jackson allow for easy composition and stringification of objects, but for this example, we have just built a custom String like:
That is compliant with the original Ontology instance JSON schema:
{"androidIoTFrame":{ "geometry":{"type":"Point", "coordinates":[9,19.3]},"email":"string","accelX":28.6,"accelY":28.6,"accelZ":28.6,"event":true}}
If you have any issue with the API please let us know at support@onesaitplatform.com.