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);
}
});
}
Aggregations