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:it.unicaradio.android.tasks.SendSongRequestAsyncTask.java

/**
 * {@inheritDoc}//w w  w  .j a va2 s  .c  om
 */
@Override
protected Response<String> doInBackground(Void... params) {
    JSONObject request = new JSONObject();
    try {
        request.put("method", "sendEmail");
        request.put("params", songRequest.toJSON(context));
    } catch (JSONException e) {
        return new Response<String>(Error.INTERNAL_GENERIC_ERROR);
    }

    String requestJSONString = request.toString();
    Log.d(TAG, MessageFormat.format("request: {0}", requestJSONString));

    byte[] postData = requestJSONString.getBytes();
    byte[] httpResult;
    try {
        httpResult = NetworkUtils.httpPost(WEB_SERVICE, postData, "application/json");
    } catch (ClientProtocolException e1) {
        return new Response<String>(Error.INTERNAL_GENERIC_ERROR);
    } catch (IOException e1) {
        return new Response<String>(Error.INTERNAL_DOWNLOAD_ERROR);
    } catch (HttpException e1) {
        return new Response<String>(Error.INTERNAL_GENERIC_ERROR);
    }

    if (httpResult == null) {
        return new Response<String>(Error.INTERNAL_GENERIC_ERROR);
    }

    String httpResultString = new String(httpResult);
    Log.d(TAG, MessageFormat.format("response: {0}", httpResultString));
    try {
        JSONObject response = new JSONObject(httpResultString);
        int errorCodeInt = response.getInt("errorCode");
        Error errorCode = Error.fromInteger(errorCodeInt);
        if (errorCode == Error.NO_ERROR) {
            return new Response<String>(httpResultString);
        } else {
            return new Response<String>(errorCode);
        }
    } catch (JSONException e) {
        return new Response<String>(Error.INTERNAL_GENERIC_ERROR);
    }
}

From source file:org.wso2.emm.agent.services.DeviceStartupIntentReceiver.java

/**
 * Initiates device notifier on device startup.
 * @param context - Application context.
 *//*from ww w . j a  v a  2s. c  o m*/
private void setRecurringAlarm(Context context) {
    this.resources = context.getApplicationContext().getResources();
    String mode = Preference.getString(context, Constants.PreferenceFlag.NOTIFIER_TYPE);
    boolean isLocked = Preference.getBoolean(context, Constants.IS_LOCKED);
    String lockMessage = Preference.getString(context, Constants.LOCK_MESSAGE);

    if (lockMessage == null || lockMessage.isEmpty()) {
        lockMessage = resources.getString(R.string.txt_lock_activity);
    }

    if (isLocked) {
        Operation lockOperation = new Operation();
        lockOperation.setId(DEFAULT_ID);
        lockOperation.setCode(Constants.Operation.DEVICE_LOCK);
        try {
            JSONObject payload = new JSONObject();
            payload.put(Constants.ADMIN_MESSAGE, lockMessage);
            payload.put(Constants.IS_HARD_LOCK_ENABLED, true);
            lockOperation.setPayLoad(payload.toString());
            OperationProcessor operationProcessor = new OperationProcessor(context);
            operationProcessor.doTask(lockOperation);
        } catch (AndroidAgentException e) {
            Log.e(TAG, "Error occurred while executing hard lock operation at the device startup");
        } catch (JSONException e) {
            Log.e(TAG, "Error occurred while building hard lock operation payload");
        }
    }

    int interval = Preference.getInt(context, context.getResources().getString(R.string.shared_pref_frequency));
    if (interval == DEFAULT_INDEX) {
        interval = DEFAULT_INTERVAL;
    }

    if (mode == null) {
        mode = Constants.NOTIFIER_LOCAL;
    }

    if (Preference.getBoolean(context, Constants.PreferenceFlag.REGISTERED)
            && Constants.NOTIFIER_LOCAL.equals(mode.trim().toUpperCase(Locale.ENGLISH))) {
        long startTime = DEFAULT_TIME_MILLISECONDS;

        Intent alarmIntent = new Intent(context, AlarmReceiver.class);
        PendingIntent recurringAlarmIntent = PendingIntent.getBroadcast(context, DEFAULT_REQUEST_CODE,
                alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, startTime, interval,
                recurringAlarmIntent);
        Log.d(TAG, "Setting up alarm manager for polling every " + interval + " milliseconds.");
    }
}

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

public void assertThatFindElementResponseHasCorrectFormat() throws Exception {
    HttpResponse response = executeCreateSessionRequest();
    SelendroidAssert.assertResponseIsRedirect(response);
    JSONObject session = parseJsonResponse(response);
    String sessionId = session.getString("sessionId");
    Assert.assertFalse(sessionId.isEmpty());

    JSONObject payload = new JSONObject();
    payload.put("using", "id");
    payload.put("value", "my_button_bar");

    String url = "http://" + host + ":" + port + "/wd/hub/session/" + sessionId + "/element";
    HttpResponse element = executeRequestWithPayload(url, HttpMethod.POST, payload.toString());
    SelendroidAssert.assertResponseIsOk(element);
}

From source file:de.dmxcontrol.programmer.EntityProgrammer.java

public void Send() {
    try {/*from   w ww  . j  ava 2s.c  om*/
        JSONObject o = new JSONObject();

        ServiceFrontend.get().sendMessage(o.toString().getBytes());
        o = null;
        if (o == null) {
            ;
        }
        return;
    } catch (Exception e) {
        Log.e("UDP Send: ", e.getMessage());
        DMXControlApplication.SaveLog();
    }
}

From source file:de.dmxcontrol.programmer.EntityProgrammer.java

public static void Clear(EntityProgrammer programmer) throws JSONException {
    if (programmer == null) {
        return;//www . ja  v  a  2s. c  om
    }
    JSONObject o = new JSONObject();
    o.put("Type", NetworkID);
    o.put("GUID", programmer.guid);
    o.put("Clear", true);

    ServiceFrontend.get().sendMessage(o.toString().getBytes());
    o = null;
    if (o == null) {
        programmer.states.clear();
        programmer.runChangeListener();
    }
    return;
}

From source file:de.dmxcontrol.programmer.EntityProgrammer.java

public static void Undo(EntityProgrammer programmer) throws JSONException {
    if (programmer == null) {
        return;/*from w w  w . ja va2s. c om*/
    }
    JSONObject o = new JSONObject();
    o.put("Type", NetworkID);
    o.put("GUID", programmer.guid);
    o.put("Undo", true);

    ServiceFrontend.get().sendMessage(o.toString().getBytes());
    o = null;
    if (o == null) {
        ;
    }
    return;
}

From source file:de.dmxcontrol.programmer.EntityProgrammer.java

public static void ClearSelection(EntityProgrammer programmer) throws JSONException {
    if (programmer == null) {
        return;/*www.j  av a 2  s . c o m*/
    }
    JSONObject o = new JSONObject();
    o.put("Type", NetworkID);
    o.put("GUID", programmer.guid);
    o.put("ClearSelection", true);

    ServiceFrontend.get().sendMessage(o.toString().getBytes());
    o = null;
    if (o == null) {
        ;
    }
    return;
}

From source file:org.exoplatform.addons.codefest.team_c.controller.KittenSaverController.java

@Resource
@Ajax// w  w  w. j  ava 2  s .  com
@MimeType.JSON
public Response changeDate(String participants, int date, int month, int year) throws JSONException {
    JSONObject json = new JSONObject();
    json.put("url", KittenSaverController_.addView(participants, date, month, year));
    return Response.ok(json.toString()).withCharset(Tools.UTF_8);
}

From source file:org.exoplatform.addons.codefest.team_c.controller.KittenSaverController.java

@Resource
@Ajax// w  w  w  .j  av  a2 s  .  c o  m
@MimeType.JSON
public Response addOption(Long start, Long end, SecurityContext securityContext) throws JSONException {
    User user = kittenSaverService.getUserByUsername(securityContext.getRemoteUser());
    TimeZone timezone = TimeZone.getTimeZone(user.getTimezone());
    Calendar sCal = Calendar.getInstance(timezone);
    sCal.setTimeInMillis(start);
    Calendar eCal = Calendar.getInstance(timezone);
    eCal.setTimeInMillis(end);

    JSONObject json = new JSONObject();
    json.put("start", format(sCal));
    json.put("end", format(eCal));

    return Response.ok(json.toString()).withCharset(Tools.UTF_8);
}

From source file:org.exoplatform.addons.codefest.team_c.controller.KittenSaverController.java

@Resource
@Ajax/* ww w  . j  a  va 2s  .  c  o m*/
@MimeType.JSON
public Response addMeeting(String title, String description, String participants, String options,
        SecurityContext securityContext) throws JSONException {
    List<String> pars = new LinkedList<String>(Arrays.asList(participants.split(",")));
    List<Long> opts = new LinkedList<Long>();
    for (String opt : options.split(",")) {
        String[] time = opt.split("/");
        Date startDate = new Date();
        startDate.setTime(Long.parseLong(time[0]));
        Date endDate = new Date();
        endDate.setTime(Long.parseLong(time[1]));
        Option o = new Option(new LinkedList<Long>(), startDate, endDate);
        kittenSaverService.createOption(o);
        opts.add(o.getId());
    }

    User user = kittenSaverService.getUserByUsername(securityContext.getRemoteUser());
    Meeting meeting = new Meeting(title, description, user, Meeting.STATUS_OPENED, pars, opts, null);
    kittenSaverService.createMeeting(meeting);

    JSONObject json = new JSONObject();
    json.put("url", KittenSaverController_.index());
    return Response.ok(json.toString()).withCharset(Tools.UTF_8);
}