Search in sources :

Example 11 with JSONException

use of org.json.JSONException 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 12 with JSONException

use of org.json.JSONException 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 13 with JSONException

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

the class ClutchAPIResponseHandler method handleSuccessMessage.

protected void handleSuccessMessage(String responseBody) {
    try {
        JSONObject jsonResponse = new JSONObject(responseBody);
        JSONObject error = jsonResponse.optJSONObject("error");
        if (error != null) {
            onFailure(null, error);
            return;
        }
        onSuccess(jsonResponse.getJSONObject("result"));
    } catch (JSONException e) {
        try {
            onFailure(e, new JSONObject(responseBody));
        } catch (JSONException e1) {
            onFailure(e, null);
        }
    }
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException)

Example 14 with JSONException

use of org.json.JSONException in project SimplifyReader by chentao0707.

the class DownloadInfo method parseProgressJSONFile.

public static DownloadInfo parseProgressJSONFile(String jsonString) {
    JSONObject o;
    try {
        if (jsonString == null || jsonString.length() == 0)
            return null;
        o = new JSONObject(jsonString.trim());
    } catch (JSONException e) {
        Logger.e("jsonToDownloadInfo", e);
        return null;
    }
    DownloadInfo info = new DownloadInfo();
    info.downloadedSize = o.optInt(KEY_downloadedsize);
    info.segDownloadedSize = o.optInt(KEY_segdownloadedsize);
    info.segId = o.optInt(KEY_segID, 1);
    info.progress = o.optDouble(KEY_progress, 0);
    return info;
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException)

Example 15 with JSONException

use of org.json.JSONException in project SimplifyReader by chentao0707.

the class DownloadInfo method jsonToDownloadInfo.

public static DownloadInfo jsonToDownloadInfo(String jsonString) {
    JSONObject o;
    try {
        if (jsonString == null || jsonString.length() == 0)
            return null;
        o = new JSONObject(jsonString.trim());
    } catch (JSONException e) {
        Logger.e("Download_DownloadInfo", "DownloadInfo#jsonToDownloadInfo()", e);
        return null;
    }
    DownloadInfo info = new DownloadInfo();
    info.title = o.optString(KEY_title);
    info.videoid = o.optString(KEY_vid);
    info.showid = o.optString(KEY_showid);
    info.format = o.optInt(KEY_format);
    info.show_videoseq = o.optInt(KEY_show_videoseq);
    info.showepisode_total = o.optInt(KEY_showepisode_total);
    info.cats = o.optString(KEY_cats);
    info.seconds = o.optInt(KEY_seconds);
    info.size = o.optLong(KEY_size);
    info.segCount = o.optInt(KEY_segcount);
    info.segsSeconds = PlayerUtil.string2int((o.optString(KEY_segsseconds)).split(","));
    info.segsSize = PlayerUtil.string2long(o.optString(KEY_segssize).split(","));
    // info.segsUrl = TextUtils.isEmpty(o.optString(KEY_segsUrl)) ? new
    // String[] {}
    // : o.optString(KEY_segsUrl).split(",");
    info.taskId = o.optString(KEY_taskid);
    info.downloadedSize = o.optInt(KEY_downloadedsize);
    info.segDownloadedSize = o.optInt(KEY_segdownloadedsize);
    info.segId = o.optInt(KEY_segID, 1);
    info.createTime = o.optLong(KEY_createtime);
    info.startTime = o.optLong(KEY_starttime);
    info.getUrlTime = o.optLong(KEY_getUrlTime);
    info.finishTime = o.optLong(KEY_finishtime);
    info.state = o.optInt(KEY_state, STATE_INIT);
    info.exceptionId = o.optInt(KEY_exceptionid);
    info.progress = o.optDouble(KEY_progress, 0);
    info.language = o.optString(KEY_language);
    info.playTime = o.optInt(KEY_playTime);
    info.lastPlayTime = o.optLong(KEY_lastPlayTime);
    info.showname = o.optString(KEY_showname);
    info.savePath = o.optString(KEY_savepath);
    return info;
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException)

Aggregations

JSONException (org.json.JSONException)1760 JSONObject (org.json.JSONObject)1346 JSONArray (org.json.JSONArray)593 IOException (java.io.IOException)315 ArrayList (java.util.ArrayList)194 HashMap (java.util.HashMap)139 Test (org.junit.Test)108 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)98 Bundle (android.os.Bundle)67 VolleyError (com.android.volley.VolleyError)66 File (java.io.File)63 DefaultGNSTest (edu.umass.cs.gnsserver.utils.DefaultGNSTest)62 Response (com.android.volley.Response)58 URL (java.net.URL)54 Map (java.util.Map)45 RandomString (edu.umass.cs.gnscommon.utils.RandomString)44 MalformedURLException (java.net.MalformedURLException)43 UnsupportedEncodingException (java.io.UnsupportedEncodingException)41 Intent (android.content.Intent)40 JsonObjectRequest (com.android.volley.toolbox.JsonObjectRequest)40