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.ibm.mobilefirst.mobileedge.abstractmodel.HumidityData.java

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

    try {//from   w ww .  jav a  2  s . c  o  m
        JSONObject data = new JSONObject();
        data.put("humidity", humidity);
        json.put("humidity", data);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return json;
}

From source file:com.ws.WS_TCS201.java

@Path("/GetID/{com}")
@JSONP(queryParam = "callback")
@GET/*from   w  w  w  .j a  v  a2s . c  om*/
@Produces({ "application/x-javascript" })
public String GetID(@QueryParam("callback") String callback, @PathParam("com") String com) {
    //JOptionPane.showMessageDialog(null, "??", "Which way?", JOptionPane.INFORMATION_MESSAGE );
    JSONObject obj1 = new JSONObject();
    LinkedList l1 = new LinkedList();
    //JSONArray l1 = new JSONArray();

    PreparedStatement prepStmt = null;

    DateFormat day = new SimpleDateFormat("yyyyMMdd");
    String tmpday = day.format(new java.util.Date());

    try {
        String cSQL = "SELECT tceemp,tcenam FROM TCSTCE "
                + "WHERE tcecom = ? AND ( tceljd=0 OR tceljd + 100 > \"" + tmpday + "\" ) "
                + "ORDER BY tceemp,tcecom ";
        prepStmt = connection.prepareStatement(cSQL);
        prepStmt.setString(1, com);
        ResultSet result = prepStmt.executeQuery();
        ResultSetMetaData rsmd = result.getMetaData();
        int numcols = rsmd.getColumnCount();

        while (result.next()) {
            LinkedHashMap m1 = new LinkedHashMap();
            for (int j = 1; j <= numcols; j++) {
                Object obj = result.getObject(j);
                m1.put(rsmd.getColumnName(j).toString(), obj.toString());
            }
            l1.add(m1);
        }
        obj1.put("record", l1);
    } catch (SQLException e) {
        prepStmt = null;
        e.printStackTrace();
    } catch (Exception e) {
        prepStmt = null;
        e.printStackTrace();
    }
    return obj1.toString();
}

From source file:com.ws.WS_TCS201.java

@Path("/GetTCD")
@JSONP(queryParam = "callback")
@GET//from w  w  w.j av  a2s.  c  om
@Produces({ "application/x-javascript" })
public String GetTCD(@QueryParam("callback") String callback) {

    JSONObject obj1 = new JSONObject();
    LinkedList l1 = new LinkedList();
    //JSONArray l1 = new JSONArray();

    PreparedStatement prepStmt = null;

    try {
        String cSQL = "SELECT tcctcd,CONCAT(tcctcd,\" - \",trim(tcctxt)) AS name FROM TCSTCC "
                + "WHERE tcctcd NOT IN (\"A\",\"L\",\"N\",\"J\",\"R\",\"E\") " + "ORDER BY tcctcd ";
        prepStmt = connection.prepareStatement(cSQL);
        ResultSet result = prepStmt.executeQuery();
        ResultSetMetaData rsmd = result.getMetaData();
        int numcols = rsmd.getColumnCount();

        while (result.next()) {
            LinkedHashMap m1 = new LinkedHashMap();
            for (int j = 1; j <= numcols; j++) {
                Object obj = result.getObject(j);
                m1.put(rsmd.getColumnName(j).toString(), obj.toString());
            }
            l1.add(m1);
        }
        obj1.put("record", l1);
    } catch (SQLException e) {
        prepStmt = null;
        e.printStackTrace();
    } catch (Exception e) {
        prepStmt = null;
        e.printStackTrace();
    }
    return obj1.toString();
}

From source file:com.ws.WS_TCS201.java

@Path("/GetDETAIL/{com}/{account}")
@JSONP(queryParam = "callback")
@GET//www . j a  v a 2s .c  o m
@Produces({ "application/x-javascript" })
public String GetDETAIL(@QueryParam("callback") String callback, @PathParam("com") String com,
        @PathParam("account") String account) {

    JSONObject obj1 = new JSONObject();
    LinkedList l1 = new LinkedList();

    PreparedStatement prepStmt = null;

    try {
        //

        String cSQL = " SELECT tceemp, tceapd, tceall, tcetkb, tcetkt FROM TCSTCE "
                + " WHERE tcecom= ? AND tceemp= ? " + " ORDER BY tceapd DESC";
        prepStmt = connection.prepareStatement(cSQL);
        prepStmt.setString(1, com);
        prepStmt.setString(2, account);
        ResultSet result = prepStmt.executeQuery();

        if (result.next()) {
            LinkedHashMap m1 = new LinkedHashMap();

            Object obj = result.getObject(2);

            //?
            m1.put("arrive", obj.toString().substring(0, 4) + "/" + obj.toString().substring(4, 6) + "/"
                    + obj.toString().substring(6, 8));

            //
            if (Integer.parseInt(obj.toString()) < 20100913) {
                m1.put("start", "01/01");
            } else {
                m1.put("start", obj.toString().substring(4, 6) + "/" + obj.toString().substring(6, 8));
            }

            //
            obj = result.getObject(3);
            m1.put("allday", obj.toString());

            l1.add(m1);
        }
        obj1.put("base", l1);

        //
        result.close();
        l1.clear();

        cSQL = " SELECT tch.tchyer,CONCAT(tch.tchtcd,\" - \",tcc.tcctxt) AS tcdnam,tch.tchdst,tch.tchded,tch.tchday,tch.tchlst,tch.tchtxt,tch.tchtcd,tch.tchtck, "
                + "        IFNULL(tchgrp.maxtck,\"\") AS maxtck, IFNULL(tchgrp.maxdst,0) AS maxdst "
                + " FROM TCSTCH AS tch "
                + " LEFT JOIN (SELECT DISTINCT tcecom,tceemp,tcenam FROM TCSTCE) AS tce "
                + "        ON tcecom=tchcom AND tce.tceemp=tch.tchemp "
                + " LEFT JOIN (SELECT tcctcd, tcctxt FROM TCSTCC ) AS tcc "
                + "        ON tcc.tcctcd=tch.tchtcd "
                + " LEFT JOIN ( SELECT tchcom,tchemp,tchyer,max(tchtck) AS maxtck,max(tchdst) AS maxdst FROM TCSTCH "
                + "             WHERE tchtcd not in (\"B\",\"T\",\"M\",\"F\",\"W\") "
                + "             GROUP BY tchcom,tchemp,tchyer ) AS tchgrp "
                + "        ON tch.tchcom = tchgrp.tchcom AND tch.tchemp = tchgrp.tchemp "
                + "       AND tch.tchyer = tchgrp.tchyer " + " WHERE tch.tchcom= ? AND tch.tchemp= ? "
                + "   AND tcc.tcctcd NOT IN (\"A\",\"L\",\"R\",\"J\",\"N\") "
                + "   AND tch.tchmrk=\" \" AND tch.tchyer >= 2014 " + " ORDER BY tch.tchemp,tch.tchdst DESC ";
        //"       tchmrk=\" \" AND tchyer >= CONV( SUBSTR(NOW( ),1,4),10,10) -1 " +
        prepStmt = connection.prepareStatement(cSQL);
        prepStmt.setString(1, com);
        prepStmt.setString(2, account);
        result = prepStmt.executeQuery();
        ResultSetMetaData rsmd = result.getMetaData();
        int numcols = rsmd.getColumnCount();

        while (result.next()) {
            LinkedHashMap m1 = new LinkedHashMap();
            for (int j = 1; j <= numcols; j++) {
                Object obj = result.getObject(j);
                m1.put(rsmd.getColumnName(j).toString(), obj.toString());
            }
            Object obj = result.getObject("tchtcd");
            String chk1 = obj.toString();
            obj = result.getObject("tchtck");
            String chk2 = obj.toString();
            obj = result.getObject("tchdst");
            String chk3 = obj.toString();
            obj = result.getObject("maxdst");
            String chk4 = obj.toString();
            if (((chk1.equals("M") || chk1.equals("F") || chk1.equals("W") || chk1.equals("B")
                    || chk1.equals("T")) && chk2.equals("-"))
                    || (!chk1.equals("M") && !chk1.equals("F") && !chk1.equals("W") && !chk1.equals("B")
                            && !chk1.equals("T") && chk3.equals(chk4))) {
                m1.put("edit", "Y");
            } else {
                m1.put("edit", "N");
            }
            l1.add(m1);
        }
        obj1.put("detail", l1);
    } catch (SQLException e) {
        prepStmt = null;
        e.printStackTrace();
    } catch (Exception e) {
        prepStmt = null;
        e.printStackTrace();
    }
    return obj1.toString();
}

From source file:com.ws.WS_TCS201.java

@Path("/Act")
@JSONP(queryParam = "callback")
@POST/*w w  w. j ava  2s  . co m*/
@Produces({ "application/x-javascript" })
public String Act(@QueryParam("callback") String callback, @FormParam("state") String state,
        @FormParam("com") String com, @FormParam("emp") String emp, @FormParam("tcd") String tcd,
        @FormParam("year") String year, @FormParam("strdate") String strdate,
        @FormParam("enddate") String enddate, @FormParam("days") String days,
        @FormParam("comment") String comment, @FormParam("oldstrdate") String oldstrdate,
        @Context HttpServletResponse servletResponse) {

    JSONObject obj1 = new JSONObject();
    LinkedList l1 = new LinkedList();
    String cSQL = "";
    int yeartkb = 0, yeartkt = 0, cnttkb = 0, cnttkt = 0;
    float cntdays = 0, allowdays = 0, tmplst = 0;
    String tmptck = "-";
    String msg = "";
    String yearStartDate = "";

    PreparedStatement prepStmt = null;

    try {
        if (!state.equals("D") && (msg.equals(""))) {
            //
            if (state.equals("I")) {
                oldstrdate = "0";
            }

            //?
            cSQL = " SELECT tceemp, tceapd, tceall, tcetkb, tcetkt FROM TCSTCE "
                    + " WHERE tcecom= ? AND tceemp= ? " + " ORDER BY tceapd DESC";
            prepStmt = connection.prepareStatement(cSQL);
            prepStmt.setString(1, com);
            prepStmt.setString(2, emp);
            ResultSet result = prepStmt.executeQuery();

            if (result.next()) {
                //
                Object obj = result.getObject(3);
                allowdays = Integer.parseInt(obj.toString());
                obj = result.getObject(4);
                yeartkb = Integer.parseInt(obj.toString());
                obj = result.getObject(5);
                yeartkt = Integer.parseInt(obj.toString());

                //***************************************************************
                if (msg.equals("")) {
                    obj = result.getObject(2);
                    yearStartDate = obj.toString();
                    if (Integer.parseInt(yearStartDate) <= 20100913) {
                        //2010.9.13?? 01/01
                        if ((Integer.parseInt(strdate.substring(4)) < Integer.parseInt("0101"))
                                && (Integer.parseInt(enddate.substring(4)) >= Integer.parseInt("0101"))) {
                            msg = "C";
                        }
                    } else {
                        //2010.9.13? ??
                        yearStartDate = year + yearStartDate.substring(4);
                        if ((Integer.parseInt(strdate) < Integer.parseInt(yearStartDate))
                                && (Integer.parseInt(enddate) >= Integer.parseInt(yearStartDate))) {
                            msg = "C";
                        }
                    }
                }
            } else {
                msg = "H";
            }

            //
            if ((Integer.parseInt(strdate) > Integer.parseInt(enddate)) && (msg.equals(""))) {
                msg = "A";
                //servletResponse.sendRedirect("/RestApache/Pages/TCS211_ErrorA.html");
                //return "N";
            }

            //??*****************************************************
            if ((state.equals("I") || (state.equals("U") && !strdate.equals(oldstrdate))) && (msg.equals(""))) {
                cSQL = "SELECT * FROM TCSTCH " + "WHERE TCHCOM=? AND TCHEMP=? AND "
                        + "      ?<=TCHDED AND ?>=TCHDST ";
                prepStmt = connection.prepareStatement(cSQL);
                prepStmt.setString(1, com);
                prepStmt.setString(2, emp);
                //prepStmt.setString(3, tcd);
                prepStmt.setString(3, strdate);
                prepStmt.setString(4, enddate);
                result = prepStmt.executeQuery();
                if (result.next()) {
                    msg = "B";
                    //servletResponse.sendRedirect("/RestApache/Pages/TCS211_ErrorB.html");
                    //return "N";
                }
            }

            //************************************************************* 
            /*
            if ((Integer.parseInt(strdate.substring(0,4)) != Integer.parseInt(enddate.substring(0,4))) && (msg.equals("")))
            {
            msg = "C:??";
            //servletResponse.sendRedirect("/RestApache/Pages/TCS211_ErrorC.html");
            //return "N";
            }*/

            //<=-
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            Calendar c1 = Calendar.getInstance();
            Calendar c2 = Calendar.getInstance();

            c1.setTime(sdf.parse(strdate));
            c2.setTime(sdf.parse(enddate));
            cntdays = c2.get(Calendar.DATE) - c1.get(Calendar.DATE) + 1;
            if ((Integer.parseInt(days) > cntdays) && (msg.equals(""))) {
                msg = "E";
                //servletResponse.sendRedirect("/RestApache/Pages/TCS211_ErrorE.html");
                //return "N";
            }

            //??
            if (tcd.equals("B") || tcd.equals("T")) {
                cSQL = " SELECT SUM(CASE WHEN tchtcd = \"B\" THEN 1 ELSE 0 END ) AS sumtkb,"
                        + "        SUM(CASE WHEN tchtcd = \"T\" AND tchtck = \"A\" THEN 1 WHEN tchtcd = \"T\" AND tchtck = \"B\" THEN 2 ELSE 0 END ) AS sumtkt "
                        + " FROM TCSTCH "
                        + " WHERE tchcom = ? AND tchemp = ? AND tchyer = ? AND tchdst <> ? AND tchtcd IN (\"B\",\"T\") AND tchtck NOT IN (\"-\",\"0\") ";
                prepStmt = connection.prepareStatement(cSQL);
                prepStmt.setString(1, com);
                prepStmt.setString(2, emp);
                prepStmt.setString(3, year);
                prepStmt.setString(4, strdate);
                result = prepStmt.executeQuery();
                if (result.next()) {
                    Object obj = result.getObject(1);
                    if (obj != null) {
                        cnttkb = Integer.parseInt(obj.toString());
                    }
                    obj = result.getObject(2);
                    if (obj != null) {
                        cnttkt = Integer.parseInt(obj.toString());
                    }
                }

                if (cnttkb > yeartkb || cnttkt > yeartkt) {
                    tmptck = "0";
                }
            } else if (tcd.equals("M") && (msg.equals(""))) {
                cSQL = " SELECT * FROM TCSTCH "
                        + " WHERE tchcom = ? AND tchemp= ? AND tchtcd = ? AND tchtck NOT IN (\"-\",\"X\") ";
                prepStmt = connection.prepareStatement(cSQL);
                prepStmt.setString(1, com);
                prepStmt.setString(2, emp);
                prepStmt.setString(3, tcd);
                result = prepStmt.executeQuery();
                if (result.next()) {
                    msg = "F";
                    //servletResponse.sendRedirect("/RestApache/Pages/TCS211_ErrorF.html");
                    //return "N";
                }
            } else if (tcd.equals("F")) {
                cSQL = " SELECT COUNT(*) AS times FROM TCSTCH "
                        + " WHERE tchcom = ? AND tchemp= ? AND tchyer = ? AND tchtcd = ? AND tchtck NOT IN (\"-\",\"X\") ";
                prepStmt = connection.prepareStatement(cSQL);
                prepStmt.setString(1, com);
                prepStmt.setString(2, emp);
                prepStmt.setString(3, year);
                prepStmt.setString(4, tcd);
                result = prepStmt.executeQuery();
                if (result.next()) {
                    Object obj = result.getObject(1);
                    if (obj != null && Integer.parseInt(obj.toString()) == 1) {
                        tmptck = "X";
                    }
                }
            }

            //?********************************************************
            if (tcd.equals("C") && (msg.equals(""))) {
                cSQL = " SELECT tchtcd, sum(tchday) AS tchday FROM TCSTCH "
                        + " WHERE tchcom = ? AND tchemp = ? " + "   AND tchyer = ? AND tchdst <> ? "
                        + "   AND tchtcd = \"C\" " + " GROUP BY tchemp ";
                prepStmt = connection.prepareStatement(cSQL);
                prepStmt.setString(1, com);
                prepStmt.setString(2, emp);
                prepStmt.setString(3, year);
                prepStmt.setString(4, oldstrdate);
                result = prepStmt.executeQuery();
                if (!result.wasNull()) {
                    Object obj = result.getObject(2);
                    cntdays = Integer.parseInt(obj.toString()) + Integer.parseInt(days);
                    if (cntdays > 5) {
                        msg = "G";
                        //servletResponse.sendRedirect("/RestApache/Pages/TCS211_ErrorG.html");
                        //return "N";
                    }
                }
            }

            if (msg.equals("")) {
                //<=?
                cSQL = " SELECT * FROM TCSTCH " + " WHERE tchcom = ? AND tchemp = ? "
                        + "   AND tchyer = ? AND tchdst <> ? " + " ORDER BY tchdst DESC ";
                prepStmt = connection.prepareStatement(cSQL);
                prepStmt.setString(1, com);
                prepStmt.setString(2, emp);
                prepStmt.setString(3, year);
                prepStmt.setString(4, oldstrdate);
                result = prepStmt.executeQuery();
                if (result.next()) {
                    Object obj = result.getObject(9);
                    tmplst = Float.parseFloat(obj.toString());
                    if (state.equals("U")) {
                        obj = result.getObject(8);
                        tmplst = tmplst + Integer.parseInt(obj.toString());
                    }
                    tmplst = tmplst - Integer.parseInt(days);
                } else {
                    tmplst = allowdays - Integer.parseInt(days);
                }

                if (tmplst < 0) {
                    msg = "D";
                    //servletResponse.sendRedirect("/RestApache/Pages/TCS211_ErrorD.html");
                    //return "N";
                }
            }
        }

        if (!msg.equals("")) {
            obj1.put("Msg", msg);
            return obj1.toString();
        }

        //
        DateFormat day = new SimpleDateFormat("yyyyMMdd");
        String upddate = day.format(new java.util.Date());
        DateFormat time = new SimpleDateFormat("HHmmss");
        String updtime = time.format(new java.util.Date());

        if (state.equals("I")) {
            cSQL = " INSERT INTO TCSTCH VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            prepStmt = connection.prepareStatement(cSQL);
            prepStmt.setString(1, com);
            prepStmt.setInt(2, Integer.parseInt(year));
            prepStmt.setString(3, emp);
            prepStmt.setString(4, tcd);
            prepStmt.setFloat(5, Float.parseFloat(String.valueOf(allowdays)));
            prepStmt.setInt(6, Integer.parseInt(strdate));
            prepStmt.setInt(7, Integer.parseInt(enddate));
            prepStmt.setFloat(8, Float.parseFloat(days));
            prepStmt.setFloat(9, tmplst);
            prepStmt.setString(10, tmptck);
            prepStmt.setString(11, "");
            prepStmt.setString(12, comment);
            prepStmt.setString(13, "WS_TCS201");
            prepStmt.setString(14, "TEST");
            prepStmt.setInt(15, Integer.parseInt(upddate));
            prepStmt.setInt(16, Integer.parseInt(updtime));

            if (prepStmt.executeUpdate() == 0) {
                obj1.put("Msg", "SI");
                return obj1.toString();
                //servletResponse.sendRedirect("/RestApache/Pages/TCS211_ErrorI.html");
                //return "N";
            }
        } else if (state.equals("U")) {
            cSQL = " UPDATE TCSTCH " + " SET tchdst = ?, " + " tchded = ?, " + " tchday = ?, " + " tchlst = ?, "
                    + " tchtxt = ?, " + " tchpg = ?, " + " tchus = ?, " + " tchdt = ?, " + " tchtm = ? "
                    + " WHERE tchcom = ? AND tchemp = ? AND tchtcd = ? AND tchdst = ? ";
            prepStmt = connection.prepareStatement(cSQL);
            prepStmt.setInt(1, Integer.parseInt(strdate));
            prepStmt.setInt(2, Integer.parseInt(enddate));
            prepStmt.setFloat(3, Float.parseFloat(days));
            prepStmt.setFloat(4, tmplst);
            prepStmt.setString(5, comment);
            prepStmt.setString(6, "WS_TCS201");
            prepStmt.setString(7, "TEST");
            prepStmt.setInt(8, Integer.parseInt(upddate));
            prepStmt.setInt(9, Integer.parseInt(updtime));
            prepStmt.setString(10, com);
            prepStmt.setString(11, emp);
            prepStmt.setString(12, tcd);
            prepStmt.setString(13, oldstrdate);
            String tmptest = prepStmt.toString();

            if (prepStmt.executeUpdate() == 0) {
                obj1.put("Msg", "SU");
                return obj1.toString();
                //servletResponse.sendRedirect("/RestApache/Pages/TCS211_ErrorU.html");
                //return "N";
            }
        } else if (state.equals("D")) {
            cSQL = " DELETE FROM TCSTCH " + " WHERE tchcom = ? AND tchemp = ? AND tchtcd = ? AND tchdst = ? ";
            prepStmt = connection.prepareStatement(cSQL);
            prepStmt.setString(1, com);
            prepStmt.setString(2, emp);
            prepStmt.setString(3, tcd);
            prepStmt.setString(4, oldstrdate);
            if (prepStmt.executeUpdate() == 0) {
                obj1.put("Msg", "SD");
                return obj1.toString();
                //servletResponse.sendRedirect("/RestApache/Pages/TCS211_ErrorD.html");
                //return "N";
            }
        }

        obj1.put("Msg", "SY");
        //servletResponse.sendRedirect("/RestApache/Pages/TCS211_Success.html");
    } catch (SQLException e) {
        prepStmt = null;
        e.printStackTrace();
    } catch (Exception e) {
        prepStmt = null;
        e.printStackTrace();
    }

    return obj1.toString();
    //return "Y";

}

From source file:ac.blitz.acme.video.VideoCaptureDeviceInfoAndroid.java

private static String getDeviceInfo() {
    try {/*from  w  ww .j a v a2 s  . c  o m*/
        JSONArray devices = new JSONArray();
        for (int i = 0; i < Camera.getNumberOfCameras(); ++i) {
            CameraInfo info = new CameraInfo();
            Camera.getCameraInfo(i, info);
            String uniqueName = deviceUniqueName(i, info);
            JSONObject cameraDict = new JSONObject();
            devices.put(cameraDict);
            List<Size> supportedSizes;
            List<int[]> supportedFpsRanges;
            try {
                Camera camera = Camera.open(i);
                Parameters parameters = camera.getParameters();
                supportedSizes = parameters.getSupportedPreviewSizes();
                supportedFpsRanges = parameters.getSupportedPreviewFpsRange();
                camera.release();
                Log.d(TAG, uniqueName);
            } catch (RuntimeException e) {
                Log.e(TAG, "Failed to open " + uniqueName + ", skipping");
                continue;
            }
            JSONArray sizes = new JSONArray();
            for (Size supportedSize : supportedSizes) {
                JSONObject size = new JSONObject();
                size.put("width", supportedSize.width);
                size.put("height", supportedSize.height);
                sizes.put(size);
            }
            // Android SDK deals in integral "milliframes per second"
            // (i.e. fps*1000, instead of floating-point frames-per-second) so we
            // preserve that through the Java->C++->Java round-trip.
            int[] mfps = supportedFpsRanges.get(supportedFpsRanges.size() - 1);
            cameraDict.put("name", uniqueName);
            cameraDict.put("front_facing", isFrontFacing(info)).put("orientation", info.orientation)
                    .put("sizes", sizes).put("min_mfps", mfps[Parameters.PREVIEW_FPS_MIN_INDEX])
                    .put("max_mfps", mfps[Parameters.PREVIEW_FPS_MAX_INDEX]);
        }
        String ret = devices.toString(2);
        return ret;
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.google.mist.plot.MainActivity.java

private void dumpSensorData() throws JSONException { //TODO(cjr): do we want JSONException here?
    File dataDir = getOrCreateSessionDir();
    File target = new File(dataDir, String.format("%s.json", mRecordingType));

    JSONObject jsonObject = new JSONObject();
    jsonObject.put("version", "1.0.0");

    boolean isCalibrated = mPullDetector.getCalibrationStatus();
    jsonObject.put("calibrated", isCalibrated);

    // Write system information
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);//from  www  . j  a v a2  s .c o  m
    JSONObject deviceData = new JSONObject();
    deviceData.put("Build.DEVICE", Build.DEVICE);
    deviceData.put("Build.MODEL", Build.MODEL);
    deviceData.put("Build.PRODUCT", Build.PRODUCT);
    deviceData.put("Build.VERSION.SDK_INT", Build.VERSION.SDK_INT);
    deviceData.put("screenResolution.X", size.x);
    deviceData.put("screenResolution.Y", size.y);
    jsonObject.put("systemInfo", deviceData);

    // Write magnetometer data
    JSONArray magnetData = new JSONArray();
    for (int i = 0; i < mSensorData.size(); i++) {
        JSONArray dataPoint = new JSONArray();
        long time = mSensorTime.get(i);
        dataPoint.put(time);
        dataPoint.put(mSensorAccuracy.get(i));
        float[] data = mSensorData.get(i);
        for (float d : data) {
            dataPoint.put(d);
        }
        magnetData.put(dataPoint);
    }
    jsonObject.put("magnetometer", magnetData);

    // Write onAccuracyChanged data
    JSONArray accuracyChangedData = new JSONArray();
    for (int i = 0; i < mAccuracyData.size(); i++) {
        JSONArray dataPoint = new JSONArray();
        long time = mAccuracyTime.get(i);
        dataPoint.put(time);
        dataPoint.put(mAccuracyData.get(i));
        accuracyChangedData.put(dataPoint);
    }
    jsonObject.put("onAccuracyChangedData", accuracyChangedData);

    // Write rotation data
    if (mRecordRotation) {
        JSONArray rotationData = new JSONArray();
        for (int i = 0; i < mSensorData.size(); i++) {
            JSONArray dataPoint = new JSONArray();
            long time = mRotationTime.get(i);
            dataPoint.put(time);
            float[] data = mRotationData.get(i);
            for (float d : data) {
                dataPoint.put(d);
            }
            rotationData.put(dataPoint);
        }
        jsonObject.put("rotation", rotationData);
    }

    // Write event labels
    JSONArray trueLabels = new JSONArray();
    for (int i = 0; i < mPositivesData.size(); i++) {
        JSONArray dataPoint = new JSONArray();
        long time = mPositivesTime.get(i);
        dataPoint.put(time);
        dataPoint.put(mPositivesData.get(i));
        trueLabels.put(dataPoint);
    }
    jsonObject.put("labels", trueLabels);

    try {
        FileWriter fw = new FileWriter(target, true);
        fw.write(jsonObject.toString());
        fw.flush();
        fw.close();
        mDumpPath = target.toString();
    } catch (IOException e) {
        Log.e(TAG, e.toString());
    }
}

From source file:com.facebook.internal.LikeActionController.java

private static String serializeToJson(LikeActionController controller) {
    JSONObject controllerJson = new JSONObject();
    try {// w  ww.j a  va  2 s . c o m
        controllerJson.put(JSON_INT_VERSION_KEY, LIKE_ACTION_CONTROLLER_VERSION);
        controllerJson.put(JSON_STRING_OBJECT_ID_KEY, controller.objectId);
        controllerJson.put(JSON_STRING_LIKE_COUNT_WITH_LIKE_KEY, controller.likeCountStringWithLike);
        controllerJson.put(JSON_STRING_LIKE_COUNT_WITHOUT_LIKE_KEY, controller.likeCountStringWithoutLike);
        controllerJson.put(JSON_STRING_SOCIAL_SENTENCE_WITH_LIKE_KEY, controller.socialSentenceWithLike);
        controllerJson.put(JSON_STRING_SOCIAL_SENTENCE_WITHOUT_LIKE_KEY, controller.socialSentenceWithoutLike);
        controllerJson.put(JSON_BOOL_IS_OBJECT_LIKED_KEY, controller.isObjectLiked);
        controllerJson.put(JSON_STRING_UNLIKE_TOKEN_KEY, controller.unlikeToken);
        if (controller.pendingCallId != null) {
            controllerJson.put(JSON_STRING_PENDING_CALL_ID_KEY, controller.pendingCallId.toString());
        }
        if (controller.pendingCallAnalyticsBundle != null) {
            JSONObject analyticsJSON = BundleJSONConverter.convertToJSON(controller.pendingCallAnalyticsBundle);
            if (analyticsJSON != null) {
                controllerJson.put(JSON_BUNDLE_PENDING_CALL_ANALYTICS_BUNDLE, analyticsJSON);
            }
        }
    } catch (JSONException e) {
        Log.e(TAG, "Unable to serialize controller to JSON", e);
        return null;
    }

    return controllerJson.toString();
}

From source file:com.citrus.mobile.RESTclient.java

private JSONObject getCookies(HttpResponse response) {
    Header[] headers = response.getAllHeaders();
    JSONObject cookies = new JSONObject();
    for (int i = 0; i < headers.length; i++) {
        Header h = headers[i];//  w ww .  j  av  a 2s.  co  m
        try {
            cookies.put(h.getName(), h.getValue());
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }
    return cookies;
}

From source file:com.citrus.mobile.RESTclient.java

private JSONObject formError(int status, String message) {
    JSONObject error = new JSONObject();
    try {/*from w w  w .java  2s. co m*/
        error.put("error", String.valueOf(status));
        error.put("message", message);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return error;
}