Example usage for org.json.simple JSONObject JSONObject

List of usage examples for org.json.simple JSONObject JSONObject

Introduction

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

Prototype

JSONObject

Source Link

Usage

From source file:com.owly.srv.RemoteBasicStatItfImpl.java

public boolean getStatusRemoteServer(String ipSrv, int clientPort) {

    URL url = null;/*from  w  w w  .  j  a v  a2s  .  co  m*/
    BufferedReader reader = null;
    StringBuilder stringBuilder;
    JSONObject objJSON = new JSONObject();
    JSONParser objParser = new JSONParser();
    String statusServer = "Enable";

    // Check status of remote server with healthCheck
    logger.debug("Check status of remote server with healthCheck");
    // Url to send to remote server
    // for example :
    // http://135.1.128.127:5000/healthCheck

    String urlToSend = "http://" + ipSrv + ":" + clientPort + "/healthCheck";
    logger.debug("URL for HTTP request :  " + urlToSend);

    HttpURLConnection connection;
    try {

        url = new URL(urlToSend);
        connection = (HttpURLConnection) url.openConnection();

        // just want to do an HTTP GET here
        connection.setRequestMethod("GET");

        // uncomment this if you want to write output to this url
        // connection.setDoOutput(true);

        // give it 2 second to respond
        connection.setReadTimeout(1 * 1000);
        connection.connect();

        // read the output from the server
        reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        stringBuilder = new StringBuilder();

        String line = null;
        while ((line = reader.readLine()) != null) {
            stringBuilder.append(line + "\n");
        }

        logger.debug("Response received : " + stringBuilder.toString());
        // map the response into a JSON object
        objJSON = (JSONObject) objParser.parse(stringBuilder.toString());
        // Check the response of the sever with the previous request
        String resServer = (String) objJSON.get("StatusServer");

        // If we dont receive the correct health checl we will save the
        // disable state in the database
        if (resServer.equals("OK")) {
            statusServer = "Enable";
        } else {
            statusServer = "Disable";
        }

        logger.debug("Status of the Server: " + statusServer);

    } catch (MalformedURLException e1) {
        logger.error("MalformedURLException : " + e1.getMessage());
        logger.error("Exception ::", e1);
        statusServer = "Disable";
        logger.debug("Status of the Server: " + statusServer);

    } catch (IOException e1) {
        logger.error("IOException : " + e1.toString());
        logger.error("Exception ::", e1);
        e1.printStackTrace();
        statusServer = "Disable";
        logger.debug("Status of the Server: " + statusServer);

    } catch (ParseException e1) {
        logger.error("ParseException : " + e1.toString());
        logger.error("Exception ::", e1);
        statusServer = "Disable";
        logger.debug("Status of the Server: " + statusServer);

    } finally {
        logger.debug("Save Status of Server in Database: " + statusServer);

    }

    if (statusServer.equals("Enable")) {
        return true;
    } else {
        return false;
    }

}

From source file:com.eduardojanuario.comente.controller.ComentarioController.java

/**
 * Trata e retorna o assunto/*from w  w  w.j ava  2 s. co  m*/
 * 
 */
@Path("/")
@Post
public void assunto(String assunto) {

    String url = Tratador.geraUrlParamAmigavel(assunto);

    JSONObject retorno = new JSONObject();
    retorno.put("retorno", "ok");
    retorno.put("url", url);

    result.use(Results.http()).body(retorno.toString());
}

From source file:com.conwet.silbops.msg.SubscribeMsg.java

@Override
@SuppressWarnings("unchecked")
public JSONObject getPayloadAsJSON() {

    JSONObject json = new JSONObject();
    json.put("subscription", subscription.toJSON());

    return json;//ww  w  .  j a  v  a 2  s . com
}

From source file:JavaCloud.Models.Token.java

public void delete() throws CoreException {
    JSONObject object = new JSONObject();
    object.put("login", login);
    object.put("pw_hash", Utils.calcHash(password, seed));
    object.put("token_id", id);
    Utils.request(address, "/user/token/delete/", object);
}

From source file:net.sf.okapi.filters.drupal.Node.java

@SuppressWarnings("unchecked")
public Node(String nid, String language, String type, String title, String body) {
    store = new JSONObject();
    store.put("nid", nid);
    store.put("language", language);
    store.put("type", type);
    store.put("title", title);
    setBody(language, body);//from  w ww . ja  v  a 2s  .  co  m
}

From source file:com.conwet.silbops.msg.AdvertiseMsg.java

@Override
@SuppressWarnings("unchecked")
public JSONObject getPayloadAsJSON() {

    JSONObject json = new JSONObject();
    json.put("advertise", advertise.toJSON());
    json.put("context", context.toJSON());
    return json;/*from  w  ww .jav  a 2s .c  om*/
}

From source file:mml.handler.json.STILDocument.java

/**
 * Add a range to the STIL Document. Must be added in sequence
 * @param r the actual range to add (NOT relative)
 * @return the added document/*  w  w w.  j a  v  a 2 s.c  om*/
 */
public JSONObject add(Range r) throws JSONException {
    JSONObject doc = new JSONObject();
    int reloff = r.offset - lastOffset;
    lastOffset = r.offset;
    doc.put(JSONKeys.NAME, r.name);
    doc.put(JSONKeys.RELOFF, reloff);
    doc.put(JSONKeys.LEN, r.len);
    if (r.removed)
        doc.put(JSONKeys.REMOVED, true);
    if (r.annotations != null && r.annotations.size() > 0) {
        ArrayList<Object> attrs = new ArrayList<Object>();
        for (int i = 0; i < r.annotations.size(); i++) {
            Annotation a = r.annotations.get(i);
            attrs.add(a.toJSONObject());
        }
        doc.put(JSONKeys.ANNOTATIONS, attrs);
    }
    ranges.add(doc);
    // remember for later update
    map.put(r, doc);
    return doc;
}

From source file:co.mcme.animations.actions.ExplosionAction.java

@Override
public JSONObject toJSON() {
    JSONObject result = new JSONObject();
    JSONObject data = new JSONObject();
    data.put("frame", frame);
    result.put("explosion", data);
    return result;
}

From source file:api.swipe.UserData.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//w ww. j  a  v a  2  s .  c o m
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    String method = request.getParameter("method");
    JSONObject resp = new JSONObject();
    Map result = null;
    switch (method) {
    case "newCard":
        result = this.createNewCardWith(request);
        break;
    case "updateCard":
        result = this.updateCardWith(request);
        break;
    case "removeCard":
        result = this.removeCardWith(request);
        break;
    case "updateStatus": // on, off, sending, receiving.
        result = this.updateUserCardStatusWith(request); // status = "on/off"
        break;
    case "sendCard":
        result = this.updateUserCardStatusWith(request); //status = "sending"
        break;
    case "receiveCard":
        result = this.receiveUserCardWith(request); //status = "receiving"
        break;
    default:
        result = new HashMap();
        break;
    }
    try (PrintWriter out = response.getWriter()) {
        resp.put("response", result);
        out.write(resp.toJSONString());
    }
}

From source file:capabilities.DevicesSO.java

@Override
public String adaptRole(String dbResult) throws Exception {

    // Variveis retornadas do WURFL em JSON
    String device_os;//from w ww .java  2 s .co m
    String device_os_version;
    String model_name;
    String brand_name;
    String is_wireless_device;
    String is_tablet;
    String pointing_method;

    // Conversao do JSON de entrada para as variaveis respectivas
    JSONObject capabilities;
    JSONParser parser = new JSONParser();
    capabilities = (JSONObject) parser.parse(dbResult);
    //JSONObject capabilities = (JSONObject) my_obj.get("capabilities");
    System.out.println("\t" + capabilities);

    device_os = (String) capabilities.get("device_os");
    device_os_version = (String) capabilities.get("device_os_version");
    model_name = (String) capabilities.get("model_name");
    brand_name = (String) capabilities.get("brand_name");
    is_wireless_device = (String) capabilities.get("is_wireless_device");
    is_tablet = (String) capabilities.get("is_tablet");
    pointing_method = (String) capabilities.get("pointing_method");

    // Criar um novo JSON e adicionar as informaes  ele.
    JSONObject virtual = new JSONObject();

    // Adicionar esse novo JSON ao JSON de entrada e retorn-lo
    capabilities.put("virtual", virtual);
    return capabilities.toJSONString();
}