To make requests against the Geoloqi REST API, you'll need an instance of LQSession
. You can easily get the active session instance by binding to the background service.
Once you've bound to the tracking service you can invoke the service's getSession method to get an instance of the active user session. Once you've done that, you can invoke any of the provided runGetRequest or runPostRequest helper methods.
// Create a new anonymous user
if (mService != null) {
LQSession session = mService.getSession();
// A unique key to identify this user
String key = "1234567890";
// An array of layer IDs to subscribe the new user to
String[] layerIds = {"xxx" ,"yyy"};
// An array of tokens specifying groups that the new user should be a member of
String[] groupTokens = {"abcdefg", "hijklmn"};
// Send the request
session.createAnonymousUserAccount(key, layerIds, groupTokens, new OnRunApiRequestListener() {
@Override
public void onSuccess(LQSession session, JSONObject json, Header[] headers) {
// Consume the response!
// ...
}
@Override
public void onFailure(LQSession session, LQException e) {
Log.e(TAG, e.getMessage());
}
@Override
public void onComplete(LQSession session, JSONObject json, Header[] headers, StatusLine status) {
// The server did not return a 200-OK response!
// ...
}, new Handler(), this);
}
// Get an account profile
if (mService != null) {
LQSession session = mService.getSession();
// Send the request
session.runGetRequest("account/profile", new OnRunApiRequestListener() {
@Override
public void onSuccess(LQSession session, JSONObject json, Header[] headers) {
// Consume the response!
// ...
}
@Override
public void onFailure(LQSession session, LQException e) {
Log.e(TAG, e.getMessage());
}
@Override
public void onComplete(LQSession session, JSONObject json, Header[] headers, StatusLine status) {
// The server did not return a 200-OK response!
// ...
}
});
}
// Create a trigger
if (mService != null) {
LQSession session = mService.getSession();
// Build the request
JSONObect trigger = new JSONObject();
try {
trigger.put("type", "message");
trigger.put("text", "Welcome to Powells Books");
trigger.put("latitude", 45.52334);
trigger.put("longitude", -122.681612);
trigger.put("radius", 300);
} catch (JSONException e) {
// Pass
}
// Send the request
session.runPostRequest("trigger/create", trigger, new OnRunAPIRequestListener() {
@Override
public void onSuccess(LQSession session, JSONObject json, Header[] headers) {
// Consume the response!
// ...
}
@Override
public void onFailure(LQSession session, LQException e) {
Log.e(TAG, e.getMessage());
}
@Override
public void onComplete(LQSession session, JSONObject json, Header[] headers, StatusLine status) {
// The server did not return a 200-OK response!
// ...
}
});
}
// Create a geonote
if (mService != null) {
LQSession session = mService.getSession();
// Build your request
JSONObject geonote = new JSONObject();
try {
geonote.put("text", "Don't forget the milk!");
geonote.put("latitude", 45.5037078163837);
geonote.put("longitude", -122.622699737549);
geonote.put("radius", 467);
geonote.put("place_name", "Grocery Store");
} catch (JSONException e) {
// Pass
}
// Send the request
session.runPostRequest("geonote/create", geonote, new OnRunAPIRequestListener() {
@Override
public void onSuccess(LQSession session, JSONObject json, Header[] headers) {
// Consume the response!
// ...
}
@Override
public void onFailure(LQSession session, LQException e) {
Log.e(TAG, e.getMessage());
}
@Override
public void onComplete(LQSession session, JSONObject json, Header[] headers, StatusLine status) {
// The server did not return a 200-OK response!
// ...
}
});
}
The API request is run asynchronously on a background Thread. Simply provide a callback listener to receive the response and you're done! The session instance automagically signs the request with the credentials of the currently authenticated user. If you're calling one of the LQSession methods that create a user, the request will be signed with your application credentials.