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:io.github.minime89.passbeam.keyboard.Keystate.java

public JSONObject dump() throws JSONException {
    JSONObject obj = new JSONObject();
    obj.put("modifiers", modifiers);

    return obj;/* w w  w  .  j a va 2s  .c  o m*/
}

From source file:com.userhook.model.UHMessageMetaImage.java

public JSONObject toJSON() {
    JSONObject json = new JSONObject();

    try {/*from  w  w w.j a  va  2 s . com*/

        if (url != null) {
            json.put("url", url);
        }

        if (height != null) {
            json.put("height", height);
        }

        if (width != null) {
            json.put("width", width);
        }
    } catch (JSONException je) {
        Log.e(UserHook.TAG, "error creating meta image json", je);
    }

    return json;
}

From source file:org.chromium.ChromeSystemDisplay.java

private JSONObject getBounds(final Display display) throws JSONException {
    JSONObject ret = new JSONObject();
    int widthPixels = 0;
    int heightPixels = 0;
    final DisplayMetrics displayMetrics = new DisplayMetrics();

    try {//  w  ww  . ja va 2  s  . c o  m
        display.getRealMetrics(displayMetrics);
    } catch (NoSuchMethodError e) {
        display.getMetrics(displayMetrics);
    }
    widthPixels = displayMetrics.widthPixels;
    heightPixels = displayMetrics.heightPixels;

    ret.put("left", 0);
    ret.put("top", 0);
    ret.put("width", widthPixels);
    ret.put("height", heightPixels);

    return ret;
}

From source file:org.chromium.ChromeSystemDisplay.java

private JSONObject getOverscan() throws JSONException {
    JSONObject ret = new JSONObject();

    ret.put("left", 0);
    ret.put("top", 0);
    ret.put("right", 0);
    ret.put("bottom", 0);

    return ret;//from   w ww  . ja  v  a  2s .  co m
}

From source file:org.chromium.ChromeSystemDisplay.java

private JSONObject getWorkArea(final Display display) throws JSONException {
    JSONObject ret = new JSONObject();
    final DisplayMetrics displayMetrics = new DisplayMetrics();
    display.getMetrics(displayMetrics);//from   w w  w.j a  v a 2  s  .  c o  m

    ret.put("left", 0);
    ret.put("top", 0);
    ret.put("width", displayMetrics.widthPixels);
    ret.put("height", displayMetrics.heightPixels);

    return ret;
}

From source file:org.chromium.ChromeSystemDisplay.java

private JSONObject getDisplayInfo(final Display display) throws JSONException {
    JSONObject displayInfo = new JSONObject();

    displayInfo.put("id", Integer.toString(display.getDisplayId()));
    try {/*from  ww  w  . j a  va2 s.com*/
        displayInfo.put("name", display.getName());
    } catch (NoSuchMethodError e) {
        displayInfo.put("name", "Default");
    }
    displayInfo.put("isPrimary", display.getDisplayId() == android.view.Display.DEFAULT_DISPLAY);
    displayInfo.put("dpiX", getDpiX(display));
    displayInfo.put("dpiY", getDpiY(display));
    displayInfo.put("rotation", getRotation(display));
    displayInfo.put("bounds", getBounds(display));
    displayInfo.put("overscan", getOverscan());
    displayInfo.put("workArea", getWorkArea(display));
    // mirroringSourceId, isInternal and isEnabled cannot be retrieved at this moment.

    return displayInfo;
}

From source file:com.pinch.console.GcmSender.java

public static void main(String[] args) {
    //        if (args.length < 1 || args.length > 2 || args[0] == null) {
    //            System.err.println("usage: ./gradlew run -Pmsg=\"MESSAGE\" [-Pto=\"DEVICE_TOKEN\"]");
    //            System.err.println("");
    //            System.err.println("Specify a test message to broadcast via GCM. If a device's GCM registration token is\n" +
    //                    "specified, the message will only be sent to that device. Otherwise, the message \n" +
    //                    "will be sent to all devices subscribed to the \"global\" topic.");
    //            System.err.println("");
    //            System.err.println("Example (Broadcast):\n" +
    //                    "On Windows:   .\\gradlew.bat run -Pmsg=\"<Your_Message>\"\n" +
    //                    "On Linux/Mac: ./gradlew run -Pmsg=\"<Your_Message>\"");
    //            System.err.println("");
    //            System.err.println("Example (Unicast):\n" +
    //                    "On Windows:   .\\gradlew.bat run -Pmsg=\"<Your_Message>\" -Pto=\"<Your_Token>\"\n" +
    //                    "On Linux/Mac: ./gradlew run -Pmsg=\"<Your_Message>\" -Pto=\"<Your_Token>\"");
    //            System.exit(1);
    //        }/*  w w  w  .j ava  2  s.c  o m*/
    try {
        // Prepare JSON containing the GCM message content. What to send and where to send.
        JSONObject jGcmData = new JSONObject();
        JSONObject jData = new JSONObject();
        jData.put("message", "Saurabh Garg signed up!!");
        jData.put("title", "New sign up!");
        // Where to send GCM message.
        //if (args.length > 1 && args[1] != null) {
        jGcmData.put("to",
                "dOEmVpNPTCY:APA91bEkeYfFMhAraF4d87SJu12oxDElUp6KW1r710KSKeuDpW31Cd5_WExUxT16KNquJ69wwDMlxd-3qoEIeDNJNU1XjyXiXztOqlKglB-zdOlszoXhX9zIYmYNV8d4vWkM0oPwjlk9");
        //            } else {
        //                jGcmData.put("to", "/topics/global");
        //            }
        // What to send in GCM message.
        jGcmData.put("data", jData);

        // Create connection to send GCM Message request.
        URL url = new URL("https://android.googleapis.com/gcm/send");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("Authorization", "key=" + API_KEY);
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);

        // Send GCM message content.
        OutputStream outputStream = conn.getOutputStream();
        outputStream.write(jGcmData.toString().getBytes());

        // Read GCM response.
        InputStream inputStream = conn.getInputStream();
        String resp = IOUtils.toString(inputStream);
        System.out.println(resp);
        System.out.println("Check your device/emulator for notification or logcat for "
                + "confirmation of the receipt of the GCM message.");
    } catch (IOException e) {
        System.out.println("Unable to send GCM message.");
        System.out.println("Please ensure that API_KEY has been replaced by the server "
                + "API key, and that the device's registration token is correct (if specified).");
        e.printStackTrace();
    }
}

From source file:com.norman0406.slimgress.API.Interface.Interface.java

public void handshake(final Handshake.Callback callback) {
    new Thread(new Runnable() {
        @Override/* w  w w .  j  av  a2s  .c o m*/
        public void run() {
            JSONObject params = new JSONObject();
            try {
                // set handshake parameters
                params.put("nemesisSoftwareVersion", mApiVersion);
                params.put("deviceSoftwareVersion", Build.VERSION.RELEASE);

                // TODO:
                /*params.put("activationCode", "");
                params.put("tosAccepted", "1");
                params.put("a", "");*/

                String paramString = params.toString();
                paramString = URLEncoder.encode(paramString, "UTF-8");

                String handshake = mApiBaseURL + mApiHandshake + paramString;

                HttpGet get = new HttpGet(handshake);
                get.setHeader("Accept-Charset", "utf-8");
                get.setHeader("Cache-Control", "max-age=0");

                // do handshake
                HttpResponse response = null;
                synchronized (Interface.this) {
                    Log.i("Interface", "executing handshake");
                    response = mClient.execute(get);
                }
                assert (response != null);
                HttpEntity entity = response.getEntity();

                if (entity != null) {
                    String content = EntityUtils.toString(entity);
                    Header contentType = entity.getContentType();
                    entity.consumeContent();

                    // check for content type json
                    if (!contentType.getName().equals("Content-Type")
                            || !contentType.getValue().contains("application/json"))
                        throw new RuntimeException("content type is not json");

                    content = content.replace("while(1);", "");

                    // handle handshake data
                    callback.handle(new Handshake(new JSONObject(content)));

                    Log.i("Interface", "handshake finished");
                }
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }).start();
}

From source file:com.norman0406.slimgress.API.Interface.Interface.java

public void request(final Handshake handshake, final String requestString, final Location playerLocation,
        final JSONObject requestParams, final RequestResult result) throws InterruptedException {
    if (!handshake.isValid() || handshake.getXSRFToken().length() == 0)
        throw new RuntimeException("handshake is not valid");

    new Thread(new Runnable() {
        public void run() {

            // create post
            String postString = mApiBaseURL + mApiRequest + requestString;
            HttpPost post = new HttpPost(postString);

            // set additional parameters
            JSONObject params = new JSONObject();
            if (requestParams != null) {
                if (requestParams.has("params"))
                    params = requestParams;
                else {
                    try {
                        params.put("params", requestParams);

                        // add persistent request parameters
                        if (playerLocation != null) {
                            String loc = String.format("%08x,%08x", playerLocation.getLatitude(),
                                    playerLocation.getLongitude());
                            params.getJSONObject("params").put("playerLocation", loc);
                            params.getJSONObject("params").put("location", loc);
                        }/*from   www .  java2  s  .  c  om*/
                        params.getJSONObject("params").put("knobSyncTimestamp", getCurrentTimestamp());

                        JSONArray collectedEnergy = new JSONArray();

                        // TODO: add collected energy guids

                        params.getJSONObject("params").put("energyGlobGuids", collectedEnergy);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                try {
                    params.put("params", null);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            try {
                StringEntity entity = new StringEntity(params.toString(), "UTF-8");
                entity.setContentType("application/json");
                post.setEntity(entity);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

            // set header
            post.setHeader("Content-Type", "application/json;charset=UTF-8");
            post.setHeader("Accept-Encoding", "gzip");
            post.setHeader("User-Agent", "Nemesis (gzip)");
            post.setHeader("X-XsrfToken", handshake.getXSRFToken());
            post.setHeader("Host", mApiBase);
            post.setHeader("Connection", "Keep-Alive");
            post.setHeader("Cookie", "SACSID=" + mCookie);

            // execute and get the response.
            try {
                HttpResponse response = null;
                String content = null;

                synchronized (Interface.this) {
                    response = mClient.execute(post);
                    assert (response != null);

                    if (response.getStatusLine().getStatusCode() == 401) {
                        // token expired or similar
                        //isAuthenticated = false;
                        response.getEntity().consumeContent();
                    } else {
                        HttpEntity entity = response.getEntity();

                        // decompress gzip if necessary
                        Header contentEncoding = entity.getContentEncoding();
                        if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip"))
                            content = decompressGZIP(entity);
                        else
                            content = EntityUtils.toString(entity);

                        entity.consumeContent();
                    }
                }

                // handle request result
                if (content != null) {
                    JSONObject json = new JSONObject(content);
                    RequestResult.handleRequest(json, result);
                }
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }).start();
}

From source file:com.grillecube.editor.ModelEditor.java

private JSONObject createDefaultJSON() {
    JSONObject json = new JSONObject();

    GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
    int width = gd.getDisplayMode().getWidth();

    width = width / 2;/*from   w  ww.  j  a v  a  2 s .c o  m*/
    json.put(ModelEditor.Config.MODEL_WINDOW_WIDTH, width);
    json.put(ModelEditor.Config.MODEL_WINDOW_HEIGHT, width / 1.6f);
    json.put(ModelEditor.Config.VSYNC, 1);
    json.put(ModelEditor.Config.SOUND, 1);

    return (json);
}