Search in sources :

Example 1 with StatRow

use of com.clutch.ClutchStats.StatRow in project clutchandroid by clutchio.

the class ClutchSync method background.

public static void background(final ClutchStats clutchStats) {
    ArrayList<StatRow> logs = clutchStats.getLogs();
    if (logs.size() == 0) {
        return;
    }
    final StatRow lastRow = logs.get(logs.size() - 1);
    JSONArray jsonLogs = new JSONArray();
    for (StatRow row : logs) {
        JSONObject rowObj = new JSONObject();
        try {
            rowObj.put("uuid", row.uuid);
            rowObj.put("ts", row.ts);
            rowObj.put("action", row.action);
            rowObj.put("data", row.data);
        } catch (JSONException e1) {
            // TODO: Don't discard the row.
            Log.e(TAG, "Could not properly encode the logs into JSON for upload to Clutch. Discarding the row.");
            continue;
        }
        jsonLogs.put(rowObj);
    }
    HashMap<String, JSONArray> params = new HashMap<String, JSONArray>();
    params.put("logs", jsonLogs);
    ClutchAPIClient.callMethod("stats", params, new ClutchAPIResponseHandler() {

        @Override
        public void onSuccess(JSONObject response) {
            if ("ok".equals(response.optString("status"))) {
                clutchStats.deleteLogs(lastRow.ts);
            } else {
                Log.e(TAG, "Failed to send the Clutch stats logs to the server.");
            }
        }

        @Override
        public void onFailure(Throwable e, JSONObject errorResponse) {
            Log.e(TAG, "Failed to send logs to Clutch: " + errorResponse);
        }
    });
}
Also used : StatRow(com.clutch.ClutchStats.StatRow) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Aggregations

StatRow (com.clutch.ClutchStats.StatRow)1 HashMap (java.util.HashMap)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1