Example usage for org.json JSONObject optString

List of usage examples for org.json JSONObject optString

Introduction

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

Prototype

public String optString(String key) 

Source Link

Document

Get an optional string associated with a key.

Usage

From source file:br.com.indigo.android.facebook.SocialFacebook.java

public void publishWithoutDialog(Activity activity, FbSimplePost post, String pageId,
        final NewObjectListener listener) {

    facebookRequest(activity, pageId + "/feed", paramsForPost(post), "POST", true, new RequestAdapter() {

        public void onFail(Throwable thr, Object state) {
            listener.onFail(thr);/*  w  w  w .  ja  v a 2  s  . co m*/
        }

        public void onComplete(JSONObject jsonResponse, Object state) {
            listener.onComplete(jsonResponse.optString("id"));
        }

        public void onCancel() {
            listener.onCancel();
        }
    });
}

From source file:br.com.indigo.android.facebook.SocialFacebook.java

private void handleFeedResponse(JSONObject jsonResponse, FeedListener listener) {
    FbSimplePost post;//from  ww w.  j a v  a2s  .c om
    ArrayList<FbSimplePost> posts = new ArrayList<FbSimplePost>();

    try {
        JSONArray objs = jsonResponse.getJSONArray("data");

        for (int i = 0; i < objs.length(); i++) {
            JSONObject obj = objs.getJSONObject(i);

            post = new FbSimplePost();
            post.setId(obj.getString("id"));

            JSONObject fromJson = obj.optJSONObject("from");
            if (fromJson != null) {
                FbSimpleUser from = new FbSimpleUser();
                from.setId(fromJson.getString("id"));
                from.setName(fromJson.getString("name"));

                post.setFrom(from);
            }

            post.setMessage(obj.optString("message"));
            post.setPicture(obj.optString("picture"));
            post.setLink(obj.optString("link"));
            post.setName(obj.optString("name"));
            post.setCaption(obj.optString("caption"));
            post.setDescription(obj.optString("description"));
            post.setSource(obj.optString("source"));
            post.setType(obj.optString("type"));

            post.setCreatedTime(new Date(obj.getLong("created_time")));
            post.setUpdatedTime(new Date(obj.getLong("updated_time")));

            JSONObject comments = obj.optJSONObject("comments");
            if (comments != null) {
                post.setNumberOfComments(comments.getInt("count"));
            }

            JSONObject likes = obj.optJSONObject("likes");
            if (likes != null) {
                post.setNumberOfLikes(likes.getInt("count"));
            }

            posts.add(post);
        }

        String nextPage = null;
        JSONObject paging = jsonResponse.optJSONObject("paging");
        if (paging != null) {
            nextPage = paging.optString("next");
        }

        listener.onComplete(posts, nextPage);

    } catch (JSONException e) {
        Util.logd(TAG, "Could not parse Json response", e);
        listener.onFail(e);
    }
}

From source file:br.com.indigo.android.facebook.SocialFacebook.java

private void handleUsersResponse(JSONObject jsonResponse, UsersListener listener) {

    FbSimpleUser user = null;//  www .  j a va  2  s.  com
    ArrayList<FbSimpleUser> friends = new ArrayList<FbSimpleUser>();

    try {
        JSONArray objs = jsonResponse.getJSONArray("data");

        for (int i = 0; i < objs.length(); i++) {
            JSONObject obj = objs.getJSONObject(i);

            user = new FbSimpleUser();
            user.setId(obj.getString("id"));
            user.setName(obj.getString("name"));

            String rsvpStatus = obj.optString("rsvp_status");

            if (rsvpStatus != null) {
                if (rsvpStatus.equals("not_replied")) {
                    user.setRSVPStatus(RSVP_STATUS.NOT_REPLIED);
                } else if (rsvpStatus.equals("attending")) {
                    user.setRSVPStatus(RSVP_STATUS.ATTENDING);
                } else if (rsvpStatus.equals("declined")) {
                    user.setRSVPStatus(RSVP_STATUS.DECLINED);
                } else if (rsvpStatus.equals("unsure")) {
                    user.setRSVPStatus(RSVP_STATUS.MAYBE);
                }
            }

            friends.add(user);
        }

        String nextPage = null;
        JSONObject paging = jsonResponse.optJSONObject("paging");
        if (paging != null) {
            nextPage = paging.optString("next");
        }

        listener.onComplete(friends, nextPage);

    } catch (JSONException e) {
        Util.logd(TAG, "Could not parse Json response", e);
        listener.onFail(e);
    }
}

From source file:br.com.indigo.android.facebook.SocialFacebook.java

private void handleEventsResponse(JSONObject jsonResponse, EventsListener listener) {

    ArrayList<FbEvent> events = new ArrayList<FbEvent>();

    try {//from  w w w  .  j a v  a2  s  .  c om
        JSONArray objs = jsonResponse.getJSONArray("data");

        for (int i = 0; i < objs.length(); i++) {
            JSONObject obj = objs.getJSONObject(i);

            events.add(parseEvent(obj));
        }

        String nextPage = null;
        JSONObject paging = jsonResponse.optJSONObject("paging");
        if (paging != null) {
            nextPage = paging.optString("next");
        }

        listener.onComplete(events, nextPage);

    } catch (JSONException e) {
        Util.logd(TAG, "Could not parse Json response", e);
        listener.onFail(e);
    }
}

From source file:br.com.indigo.android.facebook.SocialFacebook.java

private FbEvent parseEvent(JSONObject eventJson) throws JSONException {
    FbEvent event = new FbEvent();

    event.setId(eventJson.getString("id"));
    event.setName(eventJson.getString("name"));
    event.setDescription(eventJson.optString("description"));
    event.setStartTime(dateWithFacebookUnixTimestamp(eventJson.getLong("start_time")));
    event.setEndTime(dateWithFacebookUnixTimestamp(eventJson.optLong("end_time")));
    event.setLocation(eventJson.optString("location"));
    event.setPrivacy(eventJson.optString("privacy"));

    JSONObject ownerJson = eventJson.optJSONObject("owner");
    if (ownerJson != null) {
        FbSimpleUser owner = new FbSimpleUser();
        owner.setId(ownerJson.getString("id"));
        owner.setName(ownerJson.optString("name"));
        event.setOwner(owner);//w  ww. ja va 2  s .  c o  m
    }

    return event;
}

From source file:br.com.indigo.android.facebook.SocialFacebook.java

private void handleCommentsResponse(JSONObject jsonResponse, CommentsListener listener) {

    FbComment comment = null;//from  w  ww. j a  va2  s.c  o m
    ArrayList<FbComment> comments = new ArrayList<FbComment>();

    try {
        JSONArray objs = jsonResponse.getJSONArray("data");

        for (int i = 0; i < objs.length(); i++) {
            JSONObject obj = objs.getJSONObject(i);

            comment = new FbComment();
            comment.setId(obj.getString("id"));
            comment.setMessage(obj.optString("message"));
            comment.setCreatedTime(new Date(obj.optLong("created_time") * 1000));
            comment.setNumberOfLikes(obj.optInt("likes"));

            JSONObject fromJson = obj.optJSONObject("from");
            if (fromJson != null) {
                FbSimpleUser fromUser = new FbSimpleUser();
                fromUser.setId(fromJson.getString("id"));
                fromUser.setName(fromJson.optString("name"));

                comment.setFrom(fromUser);
            }

            comments.add(comment);
        }

        String nextPage = null;
        JSONObject paging = jsonResponse.optJSONObject("paging");
        if (paging != null) {
            nextPage = paging.optString("next");
        }

        listener.onComplete(comments, nextPage);

    } catch (JSONException e) {
        Util.logd(TAG, "Could not parse Json response", e);
        listener.onFail(e);
    }
}

From source file:org.onepf.oms.data.SkuDetails.java

public SkuDetails(String json) throws JSONException {
    JSONObject o = new JSONObject(json);
    _sku = o.getString("productId");
    _type = o.optString("type");
    _price = o.optString("price");
    _title = o.optString("title");
    _description = o.optString("description");
}

From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.VideoObj.java

@Override
public Pair<JSONObject, byte[]> splitRaw(JSONObject json) {
    byte[] raw = Base64.decode(json.optString(DATA));
    json.remove(DATA);/* ww  w  .  j  a v  a  2 s.  c  o  m*/
    return new Pair<JSONObject, byte[]>(json, raw);
}

From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.VideoObj.java

public Pair<JSONObject, byte[]> handleUnprocessed(Context context, JSONObject msg) {
    byte[] bytes = Base64.decode(msg.optString(DATA));
    msg.remove(DATA);//from   ww  w .  j av  a  2  s  . c o m
    return new Pair<JSONObject, byte[]>(msg, bytes);
}

From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.VideoObj.java

@Override
public Pair<JSONObject, byte[]> handleOutgoing(JSONObject json) {
    byte[] bytes = Base64.decode(json.optString(DATA));
    json.remove(DATA);//www  .  ja  v a 2s . c  o  m
    return new Pair<JSONObject, byte[]>(json, bytes);
}