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.easy.facebook.android.apicall.GraphApi.java

private List<Message> getInBoxCall(int limitInBox) throws EasyFacebookError {
    int numArrayElement = 0;
    int flagNext = 0;
    String until = "";
    int countInBox = 0;

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

    String jsonResponse;// ww w.  j  a v  a 2  s  .  c  o  m
    List<Message> messageList = new ArrayList<Message>();

    try {
        jsonResponse = Util.openUrl("https://graph.facebook.com/me/inbox", "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() && (countInBox < limitInBox || limitInBox < 0); i++) {
                    messageList.add(jsonArray.getMessage(i));
                    countInBox++;
                }

                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())
                                && (countInBox < limitInBox || limitInBox < 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/me/inbox", "GET", params);

                            objectJSONErrorCheck = new JSONObject(jsonResponse);

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

                            }
                        } else {
                            flagNext = 0;
                        }

                    }
                }
            }
        } while (flagNext == 1 && numArrayElement > 0 && (countInBox < limitInBox || limitInBox < 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 messageList;
}

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

private List<Note> getNoteCall(int limitNote) throws EasyFacebookError {
    int numArrayElement = 0;
    int flagNext = 0;
    String until = "";
    int countNote = 0;

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

    List<Note> noteList = new ArrayList<Note>();
    String jsonResponse;// www .j  a va2s .  c om
    try {
        jsonResponse = Util.openUrl("https://graph.facebook.com/me/notes", "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() && (countNote < limitNote || limitNote < 0); i++) {
                    noteList.add(jsonArray.getNote(i));
                    countNote++;
                }

                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())
                                && (countNote < limitNote || limitNote < 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/me/notes", "GET", params);

                            objectJSONErrorCheck = new JSONObject(jsonResponse);

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

                            }
                        } else {
                            flagNext = 0;
                        }

                    }
                }
            }
        } while (flagNext == 1 && numArrayElement > 0 && (countNote < limitNote || limitNote < 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 noteList;
}

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

public Page getPage(String pageId) throws EasyFacebookError {

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

    String jsonResponse;/*from w w  w  .  j  a va  2s .  c o m*/
    try {
        jsonResponse = Util.openUrl("https://graph.facebook.com/" + pageId, "GET", params);

        JSONSingleObjectDecode objectjson = new JSONSingleObjectDecode();

        JSONObject objectJSONErrorCheck = new JSONObject(jsonResponse);

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

        }

        return objectjson.getPage(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");
    }

}

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

private List<Photo> getPhotosCall(String friendID, int limitPhotos) throws EasyFacebookError {
    int numArrayElement = 0;
    int flagNext = 0;
    String until = "";
    int countPhotos = 0;

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

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

    String jsonResponse;/*from w ww  .  j a  va 2 s .c o  m*/
    List<Photo> photosList = new ArrayList<Photo>();

    try {
        jsonResponse = Util.openUrl("https://graph.facebook.com/" + friendID + "/photos", "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() && (countPhotos < limitPhotos || limitPhotos < 0); i++) {
                    photosList.add(jsonArray.getPhoto(i));
                    countPhotos++;
                }

                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())
                                && (countPhotos < limitPhotos || limitPhotos < 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 + "/photos",
                                    "GET", params);

                            objectJSONErrorCheck = new JSONObject(jsonResponse);

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

                            }
                        } else {
                            flagNext = 0;
                        }

                    }
                }
            }
        } while (flagNext == 1 && numArrayElement > 0 && (countPhotos < limitPhotos || limitPhotos < 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 photosList;
}

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

private User getUserCall(String friendID) throws EasyFacebookError {
    Bundle params = new Bundle();
    params.putString("format", "json");
    params.putString("access_token", facebook.getAccessToken());

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

    String jsonResponse;/*w  w w .j a  v a  2 s.c  o  m*/
    try {
        jsonResponse = Util.openUrl("https://graph.facebook.com/" + friendID, "GET", params);

        JSONObject objectJSONErrorCheck = new JSONObject(jsonResponse);

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

        JSONSingleObjectDecode objectjson = new JSONSingleObjectDecode();
        return objectjson.getUser(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");
    }

}

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

public List<Checkin> searchLocation(String search, String longitude, String latitude, String distance,
        int limitCheckin) throws EasyFacebookError {

    int countCheckin = 0;
    Bundle params = new Bundle();

    List<Checkin> checkinList = new ArrayList<Checkin>();

    String jsonResponse;/*from   ww w .ja  v  a2 s  .co m*/
    try {
        jsonResponse = Util.openUrl(
                "https://graph.facebook.com/search?q=" + search + "&type=place&center=" + longitude + ","
                        + latitude + "&distance=" + distance + "&access_token=" + facebook.getAccessToken(),
                "GET", params);

        JSONObject objectJSONErrorCheck = new JSONObject(jsonResponse);

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

        }

        JSONObject objectJSON = new JSONObject(jsonResponse);

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

            JSONObjectDecode jsonArray = new JSONObjectDecode(jsonData);

            for (int i = 0; i < jsonArray.length() && (countCheckin < limitCheckin || limitCheckin < 0); i++) {
                checkinList.add(jsonArray.getCheckin(i));
                countCheckin++;
            }
        }

    } 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 checkinList;

}

From source file:com.google.enterprise.connector.db.diffing.JsonDocument.java

/**
 * Copies a string-valued attribute from a JSONObject to a map of SPI
 * Value objects.// w ww.  ja va 2s  .  c  o  m
 */
private static void extractAttribute(JSONObject jo, ImmutableMap.Builder<String, List<Value>> mapBuilder,
        String key) {
    try {
        if (!jo.isNull(key)) {
            mapBuilder.put(key, ImmutableList.of(Value.getStringValue(jo.getString(key))));
        }
    } catch (JSONException e) {
        LOG.log(Level.WARNING, "Exception thrown while extracting key: " + key, e);
    }
}

From source file:com.example.android.swiperefreshlistfragment.SwipeRefreshListFragmentFragment.java

/**
 * Parsing json reponse and passing the data to feed view list adapter
 * *///from ww  w  . ja v  a  2  s  . com
private void parseJsonFeed(JSONObject response) {
    try {
        JSONArray feedArray = response.getJSONArray("feed");

        for (int i = 0; i < feedArray.length(); i++) {
            JSONObject feedObj = (JSONObject) feedArray.get(i);

            FeedItem item = new FeedItem();
            item.setId(feedObj.getInt("id"));
            item.setName(feedObj.getString("name"));

            // Image might be null sometimes
            String image = feedObj.isNull("image") ? null : feedObj.getString("image");
            item.setImge(image);
            item.setStatus(feedObj.getString("status"));
            item.setProfilePic(feedObj.getString("profilePic"));
            item.setTimeStamp(feedObj.getString("timeStamp"));

            // url might be null sometimes
            String feedUrl = feedObj.isNull("url") ? null : feedObj.getString("url");
            item.setUrl(feedUrl);

            mFeedItems.add(item);
        }

        // notify data changes to list adapater
        mFeedListAdapter.notifyDataSetChanged();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

From source file:com.snappy.couchdb.CouchDBHelper.java

/**
 * Parse user JSON objects from get user contacts call
 * //from  ww  w . j a v  a  2  s  .  c om
 * @param json
 * @return
 * @throws JSONException 
 */
public static List<User> parseUserContacts(JSONObject json) throws JSONException {

    List<User> users = null;

    if (json != null) {

        if (json.has(Const.ERROR)) {
            appLogout(null, false, isInvalidToken(json));
            return null;
        }

        users = new ArrayList<User>();

        // Get the element that holds the users ( JSONArray )
        JSONArray rows = json.getJSONArray(Const.ROWS);

        for (int i = 0; i < rows.length(); i++) {

            JSONObject row = rows.getJSONObject(i);
            if (!row.isNull(Const.DOC)) {
                JSONObject userJson = row.getJSONObject(Const.DOC);

                User user = new User();

                user = sGsonExpose.fromJson(userJson.toString(), User.class);

                if (userJson.has(Const.FAVORITE_GROUPS)) {
                    JSONArray favorite_groups = userJson.getJSONArray(Const.FAVORITE_GROUPS);

                    List<String> groups = new ArrayList<String>();

                    for (int z = 0; z < favorite_groups.length(); z++) {
                        groups.add(favorite_groups.getString(z));
                    }

                    user.setGroupIds(groups);
                }

                if (userJson.has(Const.CONTACTS)) {
                    JSONArray contacts = userJson.getJSONArray(Const.CONTACTS);

                    List<String> contactsIds = new ArrayList<String>();

                    for (int j = 0; j < contacts.length(); j++) {
                        contactsIds.add(contacts.getString(j));
                    }
                    user.setContactIds(contactsIds);
                }

                users.add(user);
            }
        }
    }

    return users;
}

From source file:com.snappy.couchdb.CouchDBHelper.java

/**
 * Parse comment JSON objects from get message comments
 * //from   w ww. j av  a2 s.  c  o m
 * @param json
 * @return
 * @throws JSONException 
 */
public static List<Comment> parseMessageComments(JSONObject json) throws JSONException {

    List<Comment> comments = null;

    if (json != null) {

        if (json.has(Const.ERROR)) {
            appLogout(null, false, isInvalidToken(json));
            return null;
        }

        comments = new ArrayList<Comment>();

        // Get the element that holds the users ( JSONArray )
        JSONArray rows = json.getJSONArray(Const.ROWS);

        for (int i = 0; i < rows.length(); i++) {

            JSONObject row = rows.getJSONObject(i);
            if (!row.isNull(Const.DOC)) {
                JSONObject commentJson = row.getJSONObject(Const.DOC);

                Comment comment = new Comment();
                comment = sGsonExpose.fromJson(commentJson.toString(), Comment.class);
                comments.add(comment);
            }
        }
    }

    return comments;
}