Android Open Source - client-android Event Message






From Project

Back to project page client-android.

License

The source code is released under:

Apache License

If you think the Android project client-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.qmonix.sdk;
/*  ww  w . ja  v  a2s. c o m*/
import java.util.ArrayList;
import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONException;

import com.qmonix.sdk.utils.Utils;


/**
 * Holds all information that is necessary to send a valid event message to the Server.
 * <p>
 * {@code EventMessage} collects event objects with a method {@code addEvent} and is able to
 * encode JSON formated legal event message with {@code toJson}.
 */
public class EventMessage {

  private ArrayList<Event> eventList = new ArrayList<Event>();


  /**
   * Adds new event to the event list.
   *
   * @param event event object to add to the list.
   * @see Event
   */
  public void addEvent(Event event) {
    if (event == null) {
      throw new IllegalArgumentException("Event cannot be null.");
    }

    this.eventList.add(event);
  }

  /**
   * Encodes event message to JSON string which meets Server protocol. Sets 'whenSent' property
   * to the time when this function is being executed.
   *
   * @return event message in JSON format.
   * @throws JSONException if fails to encode event message to JSON formatted string.
   */
  public String toJson() throws JSONException {
    JSONObject json = new JSONObject();
    JSONArray jsonEvents = new JSONArray();
    for (Event e : this.eventList) {
      jsonEvents.put(e.toJson());
    }

    json.put("events", jsonEvents);

    long time_now = Utils.getUnixTime();
    json.put("whenSent", time_now);

    return json.toString();
  }
}




Java Source Code List

com.qmonix.sample.basic.MainActivity.java
com.qmonix.sdk.EventDispatchHandler.java
com.qmonix.sdk.EventDispatcher.java
com.qmonix.sdk.EventMessage.java
com.qmonix.sdk.Event.java
com.qmonix.sdk.FireableTimingEvent.java
com.qmonix.sdk.HttpEventDispatcher.java
com.qmonix.sdk.LogEventDispatcher.java
com.qmonix.sdk.QLog.java
com.qmonix.sdk.TimingEvent.java
com.qmonix.sdk.Tracker.java
com.qmonix.sdk.VolumeEvent.java
com.qmonix.sdk.helpers.HttpHelper.java
com.qmonix.sdk.helpers.exceptions.HttpHelperException.java
com.qmonix.sdk.utils.AsyncTaskResult.java
com.qmonix.sdk.utils.Utils.java