Android Open Source - android-reddit Comment Factory






From Project

Back to project page android-reddit.

License

The source code is released under:

MIT License

If you think the Android project android-reddit listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.pocketreddit.library.things.factories;
/*  w w  w  . j a  v  a  2s  . com*/
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.pocketreddit.library.things.Comment;
import com.pocketreddit.library.things.Listing;
import com.pocketreddit.library.things.Subreddit;
import com.pocketreddit.library.things.utils.JsonToThingConverter;

public class CommentFactory implements ThingFactory {
    private static final String TAG = CommentFactory.class.getName();
    private JSONObject json;

    public CommentFactory(JSONObject json) {
        this.json = json;
    }

    public Comment createThing() throws ThingFactoryException {
        Comment comment = new Comment();

        try {
            JSONObject data = json.getJSONObject("data");
            comment.setBody(data.optString("body"));
            comment.setBodyHtml(data.optString("body_html"));
            comment.setEdited(data.optBoolean("edited", false));
            Subreddit subreddit = new Subreddit();
            subreddit.setId(data.getString("subreddit_id"));
            subreddit.setDisplayName(data.getString("subreddit"));
            comment.setSubreddit(subreddit);
            comment.setAuthorFlairCssClass(data.getString("author_flair_css_class"));
            comment.setCreated(data.getDouble("created"));
            comment.setCreatedUtc(data.getDouble("created_utc"));
            comment.setDownvotes(data.getInt("downs"));
            comment.setAuthor(data.getString("author"));
            comment.setId(data.getString("id"));
            Log.v(TAG, "comment id: " + comment.getId());
            comment.setLinkId(data.getString("link_id"));
            comment.setParentId(data.getString("parent_id"));
            comment.setLiked(data.isNull("likes") ? null : data.getBoolean("likes"));
            comment.setAuthorFlairText(data.getString("author_flair_text"));

            if (!data.isNull("num_reports"))
                comment.setNumReports(data.getInt("num_reports"));

            comment.setUpvotes(data.getInt("ups"));
            comment.setName(data.getString("name"));
            JSONObject replies = data.optJSONObject("replies");
            if (replies != null) {
                comment.setReplies(new JsonToThingConverter<Listing<Comment>>().convert(replies));
            }
        } catch (JSONException e) {
            throw new ThingFactoryException("Could not convert reply JSON objects.", e);
        }

        return comment;
    }

}




Java Source Code List

com.pocketreddit.library.Constants.java
com.pocketreddit.library.Created.java
com.pocketreddit.library.JsonParsingException.java
com.pocketreddit.library.Votable.java
com.pocketreddit.library.authentication.AuthenticationException.java
com.pocketreddit.library.authentication.Authenticator.java
com.pocketreddit.library.authentication.LiveAuthenticator.java
com.pocketreddit.library.authentication.LoginResult.java
com.pocketreddit.library.datasources.DataSourceException.java
com.pocketreddit.library.datasources.JsonDataSource.java
com.pocketreddit.library.datasources.LiveDataSource.java
com.pocketreddit.library.datasources.RedditDataSource.java
com.pocketreddit.library.net.HttpHelper.java
com.pocketreddit.library.net.NetException.java
com.pocketreddit.library.things.Account.java
com.pocketreddit.library.things.Comment.java
com.pocketreddit.library.things.Kind.java
com.pocketreddit.library.things.Link.java
com.pocketreddit.library.things.Listing.java
com.pocketreddit.library.things.Message.java
com.pocketreddit.library.things.More.java
com.pocketreddit.library.things.Subreddit.java
com.pocketreddit.library.things.Thing.java
com.pocketreddit.library.things.UserSubmittedContent.java
com.pocketreddit.library.things.factories.CommentFactory.java
com.pocketreddit.library.things.factories.LinkFactory.java
com.pocketreddit.library.things.factories.ListingFactory.java
com.pocketreddit.library.things.factories.SubredditFactory.java
com.pocketreddit.library.things.factories.ThingFactoryException.java
com.pocketreddit.library.things.factories.ThingFactory.java
com.pocketreddit.library.things.utils.JsonToThingConverter.java
com.pocketreddit.library.utils.StreamUtils.java
com.pocketreddit.library.utils.UtilsException.java