Search in sources :

Example 26 with JSONObject

use of org.json.JSONObject in project clutchandroid by clutchio.

the class ClutchStats method getABLogs.

public ArrayList<ABRow> getABLogs() {
    SQLiteDatabase db = getReadableDatabase();
    String[] args = {};
    ArrayList<ABRow> res = new ArrayList<ABRow>();
    Cursor cur = db.rawQuery("SELECT uuid, ts, data FROM ablog ORDER BY ts", args);
    cur.moveToFirst();
    while (!cur.isAfterLast()) {
        String uuid = cur.getString(0);
        double ts = cur.getDouble(1);
        JSONObject data;
        try {
            data = new JSONObject(cur.getString(2));
        } catch (JSONException e) {
            Log.w(TAG, "Could not serialize to JSON: " + cur.getString(3));
            cur.moveToNext();
            continue;
        }
        res.add(new ABRow(uuid, ts, data));
        cur.moveToNext();
    }
    db.close();
    return res;
}
Also used : JSONObject(org.json.JSONObject) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) Cursor(android.database.Cursor)

Example 27 with JSONObject

use of org.json.JSONObject in project clutchandroid by clutchio.

the class ClutchStats method setNumChoices.

public void setNumChoices(String name, int numChoices, boolean hasData) {
    JSONObject data = new JSONObject();
    try {
        data.put("action", "num-choices");
        data.put("name", name);
        data.put("num_choices", numChoices);
        data.put("has_data", hasData);
    } catch (JSONException e) {
        Log.e(TAG, "Could not create JSON for insert into testChosen (name: " + name + ", numChoices: " + numChoices + ")");
        return;
    }
    logABData(data);
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException)

Example 28 with JSONObject

use of org.json.JSONObject 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)

Example 29 with JSONObject

use of org.json.JSONObject in project clutchandroid by clutchio.

the class ClutchAB method sendABLogs.

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

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

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

Example 30 with JSONObject

use of org.json.JSONObject in project clutchandroid by clutchio.

the class ClutchAB method test.

public static void test(String name, ClutchABDataTest testInstance) {
    JSONObject metaMeta = getLatestMeta();
    if (metaMeta.length() == 0) {
        stats.testFailure(name, "no-meta");
        stats.setNumChoices(name, 0, true);
        return;
    }
    JSONObject meta = metaMeta.optJSONObject(name);
    if (meta == null) {
        stats.testFailure(name, "no-meta-name");
        stats.setNumChoices(name, 0, true);
        return;
    }
    JSONArray weights = meta.optJSONArray("weights");
    JSONArray allData = meta.optJSONArray("data");
    if (allData == null || weights == null) {
        stats.testFailure(name, "no-data");
        return;
    }
    if (weights.length() == 0) {
        return;
    }
    int choice = cachedChoice(name, doubleJSONArrayToList(weights));
    stats.testChosen(name, choice, weights.length());
    if (choice == -1) {
        return;
    }
    JSONObject data = allData.optJSONObject(choice);
    testInstance.action(data);
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray)

Aggregations

JSONObject (org.json.JSONObject)3855 JSONException (org.json.JSONException)1520 JSONArray (org.json.JSONArray)1084 Test (org.junit.Test)494 IOException (java.io.IOException)425 ArrayList (java.util.ArrayList)375 HashMap (java.util.HashMap)282 Test (org.testng.annotations.Test)173 File (java.io.File)137 Date (java.util.Date)137 ServiceException (org.b3log.latke.service.ServiceException)125 VolleyError (com.android.volley.VolleyError)103 Bundle (android.os.Bundle)102 Map (java.util.Map)95 RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)92 InputStreamReader (java.io.InputStreamReader)84 Response (com.android.volley.Response)83 BufferedReader (java.io.BufferedReader)83 List (java.util.List)76 InputStream (java.io.InputStream)70