CloudPlugs REST Library  1.0.0
for Android
CloudPlugs Android Client Library

CloudPlugs REST Client for Android is a library to perform HTTP requests to CloudPlugs servers. The official repository is https://github.com/cloudplugs/rest-android

The Cloudplugs IoT Platform

CloudPlugs is a cloud based IoT platform to enable the fast prototyping, connection, deployment and management of smart "Things" (sensors, smartphones, home appliances, etc.). Different objects can interact with each other by publishing and/or reading data on shared channels through simple communication protocols. You can learn more at http://www.cloudplugs.com/how-it-works/ , and you can sign-up on http://www.cloudplugs.com/register to start connecting your Things.

Library Guide

Installation

Just put the library source files in your Android project and you are done.
You need the the packages (and subpackages of) com.cloudplugs.

Quick usage

This section shows how to integrate the CloudPlugs REST library in an Android Activity.
Import the main ClouPlugs REST Client classes in your Android Activity source file:

In the body of onCreate method, obtain a new instance of com.cloudplugs.rest.android.RestClient:

private RestClient restClient; // the RestClient instance we are going to create
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
... // other code, if any
RestClient.create(new RestClient.Listener() {
@Override
public void onReady(RestClient rc) {
restClient = rc; // we got a RestClient asynchronously
mySetup(); // custom setup
}
});
... // other code, if any
}

Now you could configure the client instance with the right authentication credentials for obtaining a new com.cloudplugs.rest.RestManager instance:

private RestManager restManager; // the RestManager instance we are going to obtain
private void mySetup() {
// create new default options
Opts opts = new Opts();
// set the authentication credentials
opts.setAuth(myAuthId, myAuthPassword, isMaster);
// and create a new RestManager
restManager = restClient.getManager(opts);
}

Finally you are ready to perform REST requests to the server!
However it's not necessary the to setup all the options before calling restClient.getManager(opts), you could obtain the com.cloudplugs.rest.RestManager before setting any option. See com.cloudplugs.rest.Opts for further details about the available options to configure.

It's important to release the allocated Android resources before quitting the application. Just add easy cleanup code in the onDestroy method of your Activity:

@Override
public void onDestroy() {
... // other code, if any
if(restClient != null) {
restClient.destroy();
restClient = null;
}
... // other code, if any
super.onDestroy();
}

Executing HTTP requests

After you get a configured instance of com.cloudplugs.rest.RestManager, you can execute HTTP requests to the CloudPlugs server by invoking any of the request methods. For easy understanding, such methods of com.cloudplugs.rest.RestManager have names prefixed with exec. All the requests are asynchronous and are performed according to the CloudPlugs REST API documentation. Example:

// read all device properties and information:
restManager.execGetDevice(restCallback);
// set the property "myPropertyName" to the integer 123:
restManager.execSetDeviceProp("myPropertyName", 123, restCallback);

where restCallback is an instance of com.cloudplugs.rest.RestCallback will receive the response.