Example usage for org.json JSONObject toString

List of usage examples for org.json JSONObject toString

Introduction

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

Prototype

public String toString() 

Source Link

Document

Make a JSON text of this JSONObject.

Usage

From source file:io.s4.client.Driver.java

/**
 * Send a message to the adapter.//  www . ja  v  a2 s.  co  m
 * 
 * @param m
 *            message to be sent
 * @return true if and only if the message was successfully sent.
 * @throws IOException
 *             if the underlying TCP/IP socket throws an exception.
 */

public boolean send(Message m) throws IOException {

    if (!state.isConnected()) {
        if (debug) {
            System.err.println("send failed. not connected.");
        }
        return false;
    }

    String s = null;

    try {
        JSONObject json = new JSONObject();

        m.toJson(json);

        s = json.toString();

        io.send(s.getBytes());

        return true;

    } catch (JSONException e) {
        if (debug) {
            System.err.println("exception while constructing message to send: " + e);
        }
        return false;
    }
}

From source file:com.mb.android.playbackmediator.utils.Utils.java

/**
 * Builds and returns a {@link android.os.Bundle} which contains a select subset of data in the
 * {@link MediaInfo}. Since {@link MediaInfo} is not {@link android.os.Parcelable}, one can use this
 * container bundle to pass around from one activity to another.
 *
 * @param info//w  w  w. j  a v a 2s  .  c om
 * @return
 * @see <code>toMediaInfo()</code>
 */
public static Bundle fromMediaInfo(MediaInfo info) {
    if (null == info) {
        return null;
    }

    MediaMetadata md = info.getMetadata();
    Bundle wrapper = new Bundle();
    wrapper.putString(MediaMetadata.KEY_TITLE, md.getString(MediaMetadata.KEY_TITLE));
    wrapper.putString(MediaMetadata.KEY_SUBTITLE, md.getString(MediaMetadata.KEY_SUBTITLE));
    wrapper.putString(KEY_URL, info.getContentId());
    wrapper.putString(MediaMetadata.KEY_STUDIO, md.getString(MediaMetadata.KEY_STUDIO));
    wrapper.putString(KEY_CONTENT_TYPE, info.getContentType());
    wrapper.putInt(KEY_STREAM_TYPE, info.getStreamType());
    wrapper.putLong(KEY_STREAM_DURATION, info.getStreamDuration());
    if (!md.getImages().isEmpty()) {
        ArrayList<String> urls = new ArrayList<String>();
        for (WebImage img : md.getImages()) {
            urls.add(img.getUrl().toString());
        }
        wrapper.putStringArrayList(KEY_IMAGES, urls);
    }
    JSONObject customData = info.getCustomData();
    if (null != customData) {
        wrapper.putString(KEY_CUSTOM_DATA, customData.toString());
    }

    return wrapper;
}

From source file:com.vinexs.tool.XML.java

/**
 * Parse standard XML to JSON Stringify.
 *
 * @param inputStream InputStream point to a XML document.
 * @return JSONObject//ww w .  j  a v a 2 s.  c o  m
 */
public static String toJSONStringify(InputStream inputStream) {
    JSONObject json = toJSONObject(inputStream);
    if (json == null) {
        return "{}";
    }
    return json.toString();
}

From source file:org.lcog.cyclelane.TripUploader.java

private List<NameValuePair> getPostData(long tripId) throws JSONException {
    JSONObject coords = getCoordsJSON(tripId);
    JSONObject user = getUserJSON();//  w  w  w.  j a  va  2  s . com
    String deviceId = getDeviceId();
    Vector<String> tripData = getTripData(tripId);
    String notes = tripData.get(0);
    String purpose = tripData.get(1);
    String startTime = tripData.get(2);
    String endTime = tripData.get(3);

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("coords", coords.toString()));
    nameValuePairs.add(new BasicNameValuePair("user", user.toString()));
    nameValuePairs.add(new BasicNameValuePair("device", deviceId));
    nameValuePairs.add(new BasicNameValuePair("notes", notes));
    nameValuePairs.add(new BasicNameValuePair("purpose", purpose));
    nameValuePairs.add(new BasicNameValuePair("start", startTime));
    nameValuePairs.add(new BasicNameValuePair("end", endTime));
    nameValuePairs.add(new BasicNameValuePair("version", "2"));

    return nameValuePairs;
}

From source file:org.godotengine.godot.MessagingService.java

private void handleData(Map<String, String> data) {
    // TODO: Perform some action now..!
    // ...// ww  w  .  j  a v  a  2  s . c  o m

    JSONObject jobject = new JSONObject();

    try {
        for (Map.Entry<String, String> entry : data.entrySet()) {
            jobject.put(entry.getKey(), entry.getValue());
        }
    } catch (JSONException e) {
        Utils.d("JSONException: parsing, " + e.toString());
    }

    if (jobject.length() > 0) {
        KeyValueStorage.setValue("firebase_notification_data", jobject.toString());
    }
}

From source file:com.scigames.slidegame.Registration3MassActivity.java

public void onResultsSucceeded(String[] serverResponseStrings, JSONObject serverResponseJSON)
        throws JSONException {

    Log.d(TAG, "LOGIN SUCCEEDED: ");
    for (int i = 0; i < serverResponseStrings.length; i++) { //just print everything returned as a String[] for fun
        Log.d(TAG, "[" + i + "] " + serverResponseStrings[i]);
    }//from w w w.j av  a2  s . c om

    JSONObject thisStudent;
    thisStudent = serverResponseJSON.getJSONObject("student");

    Log.d(TAG, "this student: ");
    Log.d(TAG, thisStudent.toString());

    Log.d(TAG, "...onResultsSucceeded");
    Intent i = new Intent(Registration3MassActivity.this, Registration4PhotoActivity.class);
    Log.d(TAG, "new Intent");
    i.putExtra("fName", firstNameIn);
    i.putExtra("lName", lastNameIn);
    i.putExtra("studentId", serverResponseStrings[0]);
    i.putExtra("visitId", serverResponseStrings[1]);
    Log.d(TAG, "startActivity...");
    Registration3MassActivity.this.startActivity(i);
    Log.d(TAG, "...startActivity");

}

From source file:net.netheos.pcsapi.credentials.PasswordCredentials.java

@Override
public String toJson() {
    JSONObject jObj = new JSONObject();
    jObj.put(PASSWORD, password);
    return jObj.toString();
}

From source file:net.jmhertlein.mcanalytics.api.request.AuthenticationRequest.java

@Override
public String toJSON() {
    JSONObject o = new JSONObject();

    o.put("type", RequestType.AUTHENTICATION.toString());
    o.put("id", getRequestId());
    o.put("method", m.name());
    o.put("username", username);
    if (m == AuthenticationMethod.PASSWORD) {
        o.put("password", password);
        if (requestRemember) {
            o.put("remember", true);
            try {
                o.put("csr", Base64.encodeBase64String(csr.getEncoded()));
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }/*from  w  w w. j  a  v a  2  s  .c  om*/
        }
    }

    return o.toString();
}

From source file:org.skt.runtime.api.PluginResult.java

public PluginResult(Status status, JSONObject message) {
    this.status = status.ordinal();
    this.message = message.toString();
}

From source file:edu.jhu.cvrg.timeseriesstore.opentsdb.TimeSeriesRetriever.java

private static JSONArray retrieveTimeSeriesPOST(String urlString, long startEpoch, long endEpoch, String metric,
        HashMap<String, String> tags) throws OpenTSDBException {

    urlString = urlString + API_METHOD;//from   w w w.j a va  2 s . c  om
    String result = "";

    try {
        HttpURLConnection httpConnection = TimeSeriesUtility.openHTTPConnectionPOST(urlString);
        OutputStreamWriter wr = new OutputStreamWriter(httpConnection.getOutputStream());

        JSONObject mainObject = new JSONObject();
        mainObject.put("start", startEpoch);
        mainObject.put("end", endEpoch);

        JSONArray queryArray = new JSONArray();

        JSONObject queryParams = new JSONObject();
        queryParams.put("aggregator", "sum");
        queryParams.put("metric", metric);

        queryArray.put(queryParams);

        if (tags != null) {
            JSONObject queryTags = new JSONObject();

            Iterator<Entry<String, String>> entries = tags.entrySet().iterator();
            while (entries.hasNext()) {
                @SuppressWarnings("rawtypes")
                Map.Entry entry = (Map.Entry) entries.next();
                queryTags.put((String) entry.getKey(), (String) entry.getValue());
            }

            queryParams.put("tags", queryTags);
        }

        mainObject.put("queries", queryArray);
        String queryString = mainObject.toString();

        wr.write(queryString);
        wr.flush();
        wr.close();

        result = TimeSeriesUtility.readHttpResponse(httpConnection);

    } catch (IOException e) {
        throw new OpenTSDBException("Unable to connect to server", e);
    } catch (JSONException e) {
        throw new OpenTSDBException("Error on request data", e);
    }

    return TimeSeriesUtility.makeResponseJSONArray(result);
}