Android Example

We would like to make the usage of the API as easy as possible. Developers are (after the API registration) able to post events and to request context models. Everything that can be done with our API is described in detail in the API documentation. It is also described how requests can be sent. Since this can be at first to think of every detail in the implementation, we would like to offer a set of classes that can be easily integrated into an Android project.

oauth-Android-4.1.zip

To integrate this aar archive in an Android Studio project, you have to open the module settings of your project. Click the Plus-button on the top left and "Import .JAR/.AAR Package". Select the .aar file from the previously .zip and wait for the project to refresh.

After this, the functionalities and the classes of this jar archive are available to your Android application. You won't have to worry about what has to be sent to the interface or how it should be formatted or calculated. The given classes will take care of everything.

Normally, a class begins with

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
}

To get started, change the class declaration to

public class MainActivity extends Activity implements LearningContext.Listener {

This enabled this Activity to listen to events. To do this, you have to add two unimplemented methods:

@Override
public void onGETResult(String result) {
	Log.d(TAG, "GET works: " + result);
}

@Override
public void onPOSTResult(String result) {
	Log.d(TAG, "POST works: " + result);
}

@Override
public void onPUTResult(String result) {
	Log.d(TAG, "PUT works: " + result);
}

@Override
public void onDELETEResult(String result) {
	Log.d(TAG, "DELETE works: " + result);
}

In these methods, you can verify the result of a query or parse the returned data (depending on your request). But there is no event to listen for, yet. Therefore, you have to create an object of the type LearningContext. It requires the URL of the API, the username and password of one valid user, the app-ID and the app-secret (you should have received that after registering your app). Additionally, you have to register the listener (in this case, a GET and a POST listener).

protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
		
	LearningContext lc = new LearningContext(
		"http://api.learning-context.de/", 4, "testuser", "password", appId,
		appSecret);

	lc.registerGETListener(this);
	lc.registerPOSTListener(this);
}

To send a GET request (receive a context model from the API), only one additional line of code is required:

lc.get("/events/", "{\"model\":\"COMPLETE\"}");

The first parameter defines the name of the interface (in this case it is events). The second parameter is the request itself. It is a JSON string as described in the API documentation.

To send a POST request (send new events to the API), the event and its entities have to be created first. Here, we will create an event which indicates the start of an application at a given timestamp. As entities, any key-value pairs can be added to an event:

Event event = new Event("START", "APPSTART", timestamp);
Entity<String> e1 = new Entity<String>("key", "value");
event.addEntity(e1);

With the help of e.g. gson, the event can be converted into its JSON representation. Since the API requires you to send an Array of events, you can either use gson to create a JSON string of an Array of Event objects or simply put [ ] around one event. After that, the JSON string can be send analogous to the GET request:

Gson g = new Gson();
String json = "[" + g.toJson(event) + "]";

lc.post("/events/update", json);