Java Example (outdated)
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.
To integrate this jar archive in Eclipse, you have to edit the properties of your project. Go to Java Build Path and Libraries and add the jar archive (Add JARs or Add External JARs, depends on whether the jar is in your project or not). The next step is to check the jar in the tab Order and Export.
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 ContextData.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); }
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 ContextData. 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); ContextData cd = new ContextData( "http://api.learning-context.de/", 3, "testuser", "password", appId, appSecret); cd.registerGETListener(this); cd.registerPOSTListener(this); }
To send a GET request (receive a context model from the API), only one additional line of code is required:
cd.get("/events/show", "{\"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) + "]"; cd.post("/events/update", json);