Example usage for org.json JSONObject isNull

List of usage examples for org.json JSONObject isNull

Introduction

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

Prototype

public boolean isNull(String key) 

Source Link

Document

Determine if the value associated with the key is null or if there is no value.

Usage

From source file:com.imaginary.home.cloud.device.Light.java

static void mapLight(@Nonnull ControllerRelay relay, @Nonnull JSONObject json,
        @Nonnull Map<String, Object> state) throws JSONException {
    mapPoweredDevice(relay, json, state);
    state.put("deviceType", "light");
    if (json.has("color")) {
        JSONObject color = json.getJSONObject("color");
        ColorMode colorMode = null;/*from  w  w w.  ja  va2  s.c om*/
        float[] components = null;

        if (color.has("colorMode") && !color.isNull("colorMode")) {
            try {
                colorMode = ColorMode.valueOf(color.getString("colorMode"));
            } catch (IllegalArgumentException e) {
                throw new JSONException("Invalid color mode: " + color.getString("colorMode"));
            }
        }
        if (color.has("components") && !color.isNull("components")) {
            JSONArray arr = color.getJSONArray("components");
            components = new float[arr.length()];

            for (int i = 0; i < arr.length(); i++) {
                components[i] = (float) arr.getDouble(i);
            }
        }
        if (colorMode != null || components != null) {
            state.put("colorMode", colorMode);
            state.put("colorValues", components);
        }
    }
    if (json.has("brightness") && !json.isNull("brightness")) {
        state.put("brightness", (float) json.getDouble("brightness"));
    }
    if (json.has("supportsColorChanges")) {
        state.put("colorChangeSupported",
                !json.isNull("supportsColorChanges") && json.getBoolean("supportsColorChanges"));
    }
    if (json.has("supportsBrightnessChanges")) {
        state.put("dimmable",
                !json.isNull("supportsBrightnessChanges") && json.getBoolean("supportsBrightnessChanges"));
    }
    if (json.has("colorModes")) {
        JSONArray arr = json.getJSONArray("colorModes");
        ColorMode[] modes = new ColorMode[arr.length()];

        for (int i = 0; i < arr.length(); i++) {
            try {
                modes[i] = ColorMode.valueOf(arr.getString(i));
            } catch (IllegalArgumentException e) {
                throw new JSONException("Invalid color mode: " + arr.getString(i));
            }
        }
        state.put("colorModesSupported", modes);
    }
}

From source file:com.easy.facebook.android.apicall.GraphApi.java

private String noteCall(String message, String subject, String friendID) throws EasyFacebookError {

    String postID = null;//from   w  ww  . j av  a 2  s .c o m

    Bundle params = new Bundle();
    params.putString("format", "json");
    params.putString("access_token", facebook.getAccessToken());
    params.putString("message", message);
    params.putString("subject", subject);

    if (friendID == null)
        friendID = "me";

    try {

        String jsonResponse = Util.openUrl("https://graph.facebook.com/" + friendID + "/notes", "POST", params);

        JSONObject objectJSONErrorCheck = new JSONObject(jsonResponse);

        if (!objectJSONErrorCheck.isNull("error")) {
            throw new EasyFacebookError(jsonResponse);

        }

        JSONObject json = new JSONObject(jsonResponse);

        if (json.has("id"))
            postID = json.get("id").toString();

    } catch (MalformedURLException e) {

        throw new EasyFacebookError(e.toString(), "MalformedURLException");
    } catch (IOException e) {

        throw new EasyFacebookError(e.toString(), "IOException");
    } catch (JSONException e) {

        throw new EasyFacebookError(e.toString(), "JSONException");
    }

    return postID;

}

From source file:com.easy.facebook.android.apicall.GraphApi.java

public String commetReply(String message, String commentID) throws EasyFacebookError {

    String postID = null;//www  .  j a va 2 s. c o  m

    Bundle params = new Bundle();
    params.putString("format", "json");
    params.putString("access_token", facebook.getAccessToken());
    params.putString("message", message);

    try {

        String jsonResponse = Util.openUrl("https://graph.facebook.com/" + commentID + "/comments", "POST",
                params);

        JSONObject objectJSONErrorCheck = new JSONObject(jsonResponse);

        if (!objectJSONErrorCheck.isNull("error")) {
            throw new EasyFacebookError(jsonResponse);

        }

        JSONObject json = new JSONObject(jsonResponse);

        if (json.has("id"))
            postID = json.get("id").toString();

    } catch (MalformedURLException e) {

        throw new EasyFacebookError(e.toString(), "MalformedURLException");
    } catch (IOException e) {

        throw new EasyFacebookError(e.toString(), "IOException");
    } catch (JSONException e) {

        throw new EasyFacebookError(e.toString(), "JSONException");
    }

    return postID;

}

From source file:com.easy.facebook.android.apicall.GraphApi.java

private String postLocation(String message, String friendID, String longitude, String latitude,
        String pagePlaceID, ArrayList<String> userIdTag) throws EasyFacebookError {

    String postID = null;//  w w  w .  j av a2 s. c  o  m

    Bundle params = new Bundle();
    params.putString("format", "json");
    params.putString("access_token", facebook.getAccessToken());
    params.putString("message", message);
    params.putString("coordinates",
            "{\"latitude\": \"" + latitude + "\", \"longitude\": \"" + longitude + "\"}");
    params.putString("place", pagePlaceID);

    String tag = "";
    int flagtag = 0;
    for (String idtag : userIdTag) {
        tag = tag + "," + idtag;
        flagtag = 1;
    }

    if (flagtag == 1) {
        tag = tag.substring(1, tag.length());
    }

    params.putString("tags", tag);

    if (friendID == null)
        friendID = "me";

    try {

        String jsonResponse = Util.openUrl("https://graph.facebook.com/" + friendID + "/checkins", "POST",
                params);

        JSONObject objectJSONErrorCheck = new JSONObject(jsonResponse);

        if (!objectJSONErrorCheck.isNull("error")) {
            throw new EasyFacebookError(jsonResponse);

        }

        JSONObject json = new JSONObject(jsonResponse);

        if (json.has("id"))
            postID = json.get("id").toString();

    } catch (MalformedURLException e) {

        throw new EasyFacebookError(e.toString(), "MalformedURLException");
    } catch (IOException e) {

        throw new EasyFacebookError(e.toString(), "IOException");
    } catch (JSONException e) {

        throw new EasyFacebookError(e.toString(), "JSONException");
    }

    return postID;

}

From source file:com.easy.facebook.android.apicall.GraphApi.java

private String postCall(String message, String friendID, String urlPicture, String actionsUrl,
        String actionsName, String link, String name, String privacy, String countries, String regions,
        String locales, String description) throws EasyFacebookError {

    String postID = null;/* w  w w  .j  ava  2 s  . co  m*/

    Bundle params = new Bundle();
    params.putString("format", "json");
    params.putString("access_token", facebook.getAccessToken());
    params.putString("message", message);

    if (description != null)
        params.putString("description", description);

    if (actionsUrl != null && actionsName != null)
        params.putString("actions", "{\"name\": \"" + actionsName + "\", \"link\": \"" + actionsUrl + "\"}");

    if (name != null)
        params.putString("name", name);

    if (link != null)
        params.putString("link", link);

    if (privacy != null)
        params.putString("privacy", privacy);

    if (countries != null && regions != null && locales != null)
        params.putString("targeting", "{\"countries\": \"" + countries + "\", \"regions\": \"" + regions
                + "\", \"locales\": \"" + locales + "\"}");

    if (urlPicture != null)
        params.putString("picture", urlPicture);

    if (friendID == null)
        friendID = "me";

    try {

        String jsonResponse = Util.openUrl("https://graph.facebook.com/" + friendID + "/feed", "POST", params);

        JSONObject objectJSONErrorCheck = new JSONObject(jsonResponse);

        if (!objectJSONErrorCheck.isNull("error")) {
            throw new EasyFacebookError(jsonResponse);

        }

        JSONObject json = new JSONObject(jsonResponse);

        if (json.has("id"))
            postID = json.get("id").toString();

    } catch (MalformedURLException e) {

        throw new EasyFacebookError(e.toString(), "MalformedURLException");
    } catch (IOException e) {

        throw new EasyFacebookError(e.toString(), "IOException");
    } catch (JSONException e) {

        throw new EasyFacebookError(e.toString(), "JSONException");
    }

    return postID;

}

From source file:com.easy.facebook.android.apicall.GraphApi.java

private String uploadPhotoCall(String message, String friendIDorAlbumID, String urlPicture)
        throws EasyFacebookError {

    String postID = null;// w w w .java  2 s .co m

    Bundle params = new Bundle();
    params.putString("format", "json");
    params.putString("access_token", facebook.getAccessToken());
    params.putString("message", message);

    if (urlPicture != null) {
        String pictureName = urlPicture.substring(urlPicture.lastIndexOf('/'), urlPicture.length());
        params.putByteArray(pictureName, Util.loadPicture(urlPicture));
    }

    if (friendIDorAlbumID == null)
        friendIDorAlbumID = "me";

    try {

        String jsonResponse = Util.openUrl("https://graph.facebook.com/" + friendIDorAlbumID + "/photos",
                "POST", params);

        JSONObject objectJSONErrorCheck = new JSONObject(jsonResponse);

        if (!objectJSONErrorCheck.isNull("error")) {
            throw new EasyFacebookError(jsonResponse);

        }

        JSONObject json = new JSONObject(jsonResponse);

        if (json.has("id"))
            postID = json.get("id").toString();

    } catch (MalformedURLException e) {

        throw new EasyFacebookError(e.toString(), "MalformedURLException");
    } catch (IOException e) {

        throw new EasyFacebookError(e.toString(), "IOException");
    } catch (JSONException e) {

        throw new EasyFacebookError(e.toString(), "JSONException");
    }

    return postID;

}

From source file:com.easy.facebook.android.apicall.GraphApi.java

private String createEventsCall(long startTime, long endTime, String name, String urlPicture, String privacy,
        String description, String location, String street, String city, String state, String country,
        String latitude, String longitude, String zip) throws EasyFacebookError {

    String eventID = null;/*w w w .  j  a v  a 2  s  .  c  o  m*/

    Bundle params = new Bundle();
    params.putString("format", "json");
    params.putString("access_token", facebook.getAccessToken());
    params.putString("start_time", Long.toString(startTime).substring(0, 10));
    params.putString("end_time", Long.toString(endTime).substring(0, 10));
    params.putString("name", name);
    params.putString("description", description);
    params.putString("location", location);
    params.putString("street", street);
    params.putString("city", city);
    params.putString("state", state);
    params.putString("country", country);
    params.putString("latitude", latitude);
    params.putString("longitude", longitude);
    params.putString("zip", zip);
    params.putString("privacy", privacy);

    if (urlPicture != null) {
        String pictureName = urlPicture.substring(urlPicture.lastIndexOf('/'), urlPicture.length());
        params.putByteArray(pictureName, Util.loadPicture(urlPicture));
    }

    try {

        String jsonResponse = Util.openUrl("https://graph.facebook.com/me/events", "POST", params);

        JSONObject objectJSONErrorCheck = new JSONObject(jsonResponse);

        if (!objectJSONErrorCheck.isNull("error")) {
            throw new EasyFacebookError(jsonResponse);

        }

        JSONObject json = new JSONObject(jsonResponse);

        if (json.has("id"))
            eventID = json.get("id").toString();

    } catch (MalformedURLException e) {

        throw new EasyFacebookError(e.toString(), "MalformedURLException");
    } catch (IOException e) {

        throw new EasyFacebookError(e.toString(), "IOException");
    } catch (JSONException e) {

        throw new EasyFacebookError(e.toString(), "JSONException");
    }

    return eventID;

}

From source file:com.easy.facebook.android.apicall.GraphApi.java

private String createAlbumCall(String albumName, String description, String location) throws EasyFacebookError {
    String albumID = "";
    Bundle params = new Bundle();
    params.putString("format", "json");
    params.putString("access_token", facebook.getAccessToken());

    params.putString("name", albumName);
    params.putString("message", description);
    params.putString("location", location);

    String jsonResponse;/*from w  w w  . j a v a 2  s .  c om*/
    try {
        jsonResponse = Util.openUrl("https://graph.facebook.com/me/albums", "POST", params);

        JSONObject objectJSONErrorCheck = new JSONObject(jsonResponse);

        if (!objectJSONErrorCheck.isNull("error")) {
            throw new EasyFacebookError(jsonResponse);

        }

        JSONObject json = new JSONObject(jsonResponse);

        if (json.has("id"))
            albumID = json.get("id").toString();

    } catch (MalformedURLException e) {

        throw new EasyFacebookError(e.toString(), "MalformedURLException");
    } catch (IOException e) {

        throw new EasyFacebookError(e.toString(), "IOException");
    } catch (JSONException e) {

        throw new EasyFacebookError(e.toString(), "JSONException");
    }

    return albumID;
}

From source file:com.easy.facebook.android.apicall.GraphApi.java

public String addMemberToFriendList(String frirndListID, String friendID) throws EasyFacebookError {

    Bundle params = new Bundle();
    params.putString("format", "json");
    params.putString("access_token", facebook.getAccessToken());

    String jsonResponse = null;/*w ww . j a  v  a 2s.com*/
    try {
        jsonResponse = Util.openUrl("https://graph.facebook.com/" + frirndListID + "/members/" + friendID,
                "POST", params);

        JSONObject objectJSONErrorCheck = new JSONObject(jsonResponse);

        if (!objectJSONErrorCheck.isNull("error")) {
            throw new EasyFacebookError(jsonResponse);

        }

    } catch (MalformedURLException e) {

        throw new EasyFacebookError(e.toString(), "MalformedURLException");
    } catch (IOException e) {

        throw new EasyFacebookError(e.toString(), "IOException");
    } catch (JSONException e) {

        throw new EasyFacebookError(e.toString(), "JSONException");
    }

    return jsonResponse;
}

From source file:com.easy.facebook.android.apicall.GraphApi.java

private List<Album> getAlbumsCall(String friendID, int limitAlbum) throws EasyFacebookError {
    int numArrayElement = 0;
    int flagNext = 0;
    String until = "";
    int countAlbum = 0;

    Bundle params = new Bundle();
    params.putString("format", "json");
    params.putString("access_token", facebook.getAccessToken());

    if (friendID == null)
        friendID = "me";

    String jsonResponse;/*from  www .java  2s.  c o  m*/
    List<Album> albumList = new ArrayList<Album>();

    try {
        jsonResponse = Util.openUrl("https://graph.facebook.com/" + friendID + "/albums", "GET", params);

        JSONObject objectJSONErrorCheck = new JSONObject(jsonResponse);

        if (!objectJSONErrorCheck.isNull("error")) {
            throw new EasyFacebookError(jsonResponse);

        }

        do {

            JSONObject objectJSON = new JSONObject(jsonResponse);

            String jsonData = "";
            if (!objectJSON.isNull("data")) {
                jsonData = objectJSON.getString("data").toString();

                JSONObjectDecode jsonArray = new JSONObjectDecode(jsonData);

                numArrayElement = jsonArray.length();

                for (int i = 0; i < jsonArray.length() && (countAlbum < limitAlbum || limitAlbum < 0); i++) {
                    albumList.add(jsonArray.getAlbum(i));
                    countAlbum++;
                }

                String paging = "";
                if (!objectJSON.isNull("paging")) {
                    paging = objectJSON.getString("paging");

                    JSONObject objectnextJSON = new JSONObject(paging);
                    String next = "";
                    if (!objectnextJSON.isNull("next")) {
                        next = objectnextJSON.getString("next").toString();
                        if (!until.equals(
                                next.substring(next.lastIndexOf("until=") + 6, next.length()).toString())
                                && (countAlbum < limitAlbum || limitAlbum < 0)) {
                            flagNext = 1;
                            until = next.substring(next.lastIndexOf("until=") + 6, next.length());
                            params = new Bundle();
                            params.putString("format", "json");
                            params.putString("access_token", facebook.getAccessToken());
                            params.putString("limit", "25");
                            params.putString("until", until);
                            jsonResponse = Util.openUrl("https://graph.facebook.com/" + friendID + "/albums",
                                    "GET", params);

                            objectJSONErrorCheck = new JSONObject(jsonResponse);

                            if (!objectJSONErrorCheck.isNull("error")) {
                                throw new EasyFacebookError(jsonResponse);

                            }
                        } else {
                            flagNext = 0;
                        }

                    }
                }
            }
        } while (flagNext == 1 && numArrayElement > 0 && (countAlbum < limitAlbum || limitAlbum < 0));

    } catch (MalformedURLException e) {

        throw new EasyFacebookError(e.toString(), "MalformedURLException");
    } catch (IOException e) {

        throw new EasyFacebookError(e.toString(), "IOException");
    } catch (JSONException e) {

        throw new EasyFacebookError(e.toString(), "JSONException");
    }

    return albumList;
}