Example usage for org.json JSONObject put

List of usage examples for org.json JSONObject put

Introduction

In this page you can find the example usage for org.json JSONObject put.

Prototype

public JSONObject put(String key, Object value) throws JSONException 

Source Link

Document

Put a key/value pair in the JSONObject.

Usage

From source file:com.chaosinmotion.securechat.server.commands.ForgotPassword.java

/**
 * Process a forgot password request. This generates a token that the
 * client is expected to return with the change password request.
 * @param requestParams//from w ww.ja va 2 s.c o m
 * @throws SQLException 
 * @throws IOException 
 * @throws ClassNotFoundException 
 * @throws JSONException 
 * @throws NoSuchAlgorithmException 
 */

public static void processRequest(JSONObject requestParams)
        throws SQLException, ClassNotFoundException, IOException, NoSuchAlgorithmException, JSONException {
    String username = requestParams.optString("username");

    /*
     * Step 1: Convert username to the userid for this
     */
    Connection c = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    int userID = 0;
    String retryID = UUID.randomUUID().toString();

    try {
        c = Database.get();
        ps = c.prepareStatement("SELECT userid " + "FROM Users " + "WHERE username = ?");
        ps.setString(1, username);
        rs = ps.executeQuery();
        if (rs.next()) {
            userID = rs.getInt(1);
        }

        if (userID == 0)
            return;
        ps.close();
        rs.close();

        /*
         * Step 2: Generate the retry token and insert into the forgot 
         * database with an expiration date 1 hour from now.
         */

        Timestamp ts = new Timestamp(System.currentTimeMillis() + 3600000);
        ps = c.prepareStatement("INSERT INTO ForgotPassword " + "    ( userid, token, expires ) " + "VALUES "
                + "    ( ?, ?, ?)");
        ps.setInt(1, userID);
        ps.setString(2, retryID);
        ps.setTimestamp(3, ts);
        ps.execute();
    } finally {
        if (rs != null)
            rs.close();
        if (ps != null)
            ps.close();
        if (c != null)
            c.close();
    }

    /*
     * Step 3: formulate a JSON string with the retry and send
     * to the user. The format of the command we send is:
     * 
     * { "cmd": "forgotpassword", "token": token }
     */

    JSONObject obj = new JSONObject();
    obj.put("cmd", "forgotpassword");
    obj.put("token", retryID);
    MessageQueue.getInstance().enqueueAdmin(userID, obj.toString(4));
}

From source file:example.SendNumbers.java

/**
 * @param args/*from w  w  w. j  av a  2s .  c  o m*/
 * @throws MalformedURLException
 */
public static void main(String[] args) throws MalformedURLException {

    AEONSDK sdk = new AEONSDK(Config.PUB_URL);

    for (int i = 0; i <= 500; i++) {
        JSONObject data = new JSONObject();
        try {
            data.put("number", i);
            System.out.println(data.toString());
            sdk.publish(data, new MyAEONCallbacks());
            TimeUnit.MICROSECONDS.sleep(200);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

From source file:github.popeen.dsub.util.SongDBHandler.java

public JSONArray exportData() {
    SQLiteDatabase db = this.getReadableDatabase();
    String[] columns = { SONGS_ID, SONGS_SERVER_KEY, SONGS_SERVER_ID, SONGS_COMPLETE_PATH, SONGS_LAST_PLAYED,
            SONGS_LAST_COMPLETED };//  w  w w  .  j  av a 2  s.  c om
    Cursor cursor = db.query(TABLE_SONGS, columns, SONGS_LAST_PLAYED + " != ''", null, null, null, null, null);
    try {
        JSONArray jsonSongDb = new JSONArray();
        while (cursor.moveToNext()) {
            JSONObject tempJson = new JSONObject();
            tempJson.put("SONGS_ID", cursor.getInt(0));
            tempJson.put("SONGS_SERVER_KEY", cursor.getInt(1));
            tempJson.put("SONGS_SERVER_ID", cursor.getString(2));
            tempJson.put("SONGS_COMPLETE_PATH", cursor.getString(3));
            tempJson.put("SONGS_LAST_PLAYED", cursor.getInt(4));
            tempJson.put("SONGS_LAST_COMPLETED", cursor.getInt(5));
            jsonSongDb.put(tempJson);
        }
        cursor.close();
        return jsonSongDb;
    } catch (Exception e) {
    }
    return new JSONArray();
}

From source file:com.stockbrowser.view.BookmarkExpandableView.java

public JSONObject saveGroupState() throws JSONException {
    JSONObject obj = new JSONObject();
    int count = mAdapter.getGroupCount();
    for (int i = 0; i < count; i++) {
        String acctName = mAdapter.mGroups.get(i);
        if (!isGroupExpanded(i)) {
            obj.put(acctName != null ? acctName : LOCAL_ACCOUNT_NAME, false);
        }//from   ww  w. j  a v  a  2 s .c o m
    }
    return obj;
}

From source file:com.ibm.mobilefirst.mobileedge.abstractmodel.GyroscopeData.java

@Override
public JSONObject asJSON() {
    JSONObject json = super.asJSON();

    try {/*from  w w w .  j a  v  a  2  s  .  c om*/
        JSONObject data = new JSONObject();
        data.put("x", x);
        data.put("y", y);
        data.put("z", z);
        json.put("gyroscope", data);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return json;
}

From source file:io.selendroid.server.SelendroidResponse.java

@Override
public String render() {
    JSONObject o = new JSONObject();
    try {/*  ww w . j a  v a  2  s  .c  o m*/
        if (sessionId != null) {
            o.put("sessionId", sessionId);
        }
        o.put("status", status);
        if (value != null) {
            o.put("value", value);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return o.toString();
}

From source file:io.selendroid.server.SelendroidResponse.java

private JSONObject buildErrorValue(Throwable t) throws JSONException {
    JSONObject errorValue = new JSONObject();
    errorValue.put("class", t.getClass().getCanonicalName());

    // TODO: Form exception in a way that will be unpacked nicely on the local end.
    StringWriter stringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stringWriter);
    t.printStackTrace(printWriter);//from w  w  w  . java 2  s  . com
    errorValue.put("message", t.getMessage() + "\n" + stringWriter.toString());

    /*
     * There is no easy way to attach exception 'cause' clauses here.
     * See workaround above which is used instead.
     */
    //      JSONArray stackTrace = new JSONArray();
    //      for (StackTraceElement el : t.getStackTrace()) {
    //          JSONObject frame = new JSONObject();
    //          frame.put("lineNumber", el.getLineNumber());
    //          frame.put("className", el.getClassName());
    //          frame.put("methodName", el.getMethodName());
    //          frame.put("fileName", el.getFileName());
    //          stackTrace.put(frame);
    //      }
    //      errorValue.put("stackTrace", stackTrace);

    return errorValue;
}

From source file:org.searsia.web.SearsiaApplication.java

protected static Response responseOk(JSONObject json) {
    json.put("searsia", VERSION);
    return Response.ok(json.toString()).header("Access-Control-Allow-Origin", "*").build();
}

From source file:org.searsia.web.SearsiaApplication.java

protected static Response responseError(int status, String error) {
    JSONObject json = new JSONObject();
    json.put("searsia", VERSION);
    json.put("error", error);
    String entity = json.toString();
    return Response.status(status).entity(entity).header("Access-Control-Allow-Origin", "*").build();
}

From source file:org.searsia.web.SearsiaApplication.java

protected static Response jsonResponse(int status, JSONObject json) {
    json.put("searsia", VERSION);
    String entity = json.toString();
    return Response.status(status).entity(entity).header("Access-Control-Allow-Origin", "*").build();
}