Android Open Source - android-reddit Live Data Source






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.datasources;
// w  w  w.j a  va  2s.c  o m
import java.util.ArrayList;
import java.util.List;

import org.apache.http.cookie.Cookie;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.json.JSONException;
import org.json.JSONObject;

import com.pocketreddit.library.net.HttpHelper;
import com.pocketreddit.library.net.NetException;

public class LiveDataSource implements JsonDataSource {
    private static final String COOKIE_KEY_REDDIT_SESSION = "reddit_session";
    private static final String URI_BASE = "http://www.reddit.com";
    private static final String URI_SUBREDDIT_PREFIX = URI_BASE + "/r/";
    private static final String URI_SUBREDDITS = URI_BASE + "/reddits.json";

    public JSONObject getSubreddits(String sessionId) throws DataSourceException {
        Cookie cookie = new BasicClientCookie(COOKIE_KEY_REDDIT_SESSION, sessionId);
        List<Cookie> cookies = new ArrayList<Cookie>();
        cookies.add(cookie);
        try {
            return HttpHelper.getInstance().getJsonObjectFromGet(URI_SUBREDDITS, cookies);
        } catch (NetException e) {
            throw new DataSourceException(
                    "error trying to parse JSON from GET @ " + URI_SUBREDDITS, e);
        }
    }

    public JSONObject getLinks(String subreddit) throws DataSourceException {
        String subredditUrl = URI_SUBREDDIT_PREFIX + subreddit + ".json";
        try {
            return HttpHelper.getInstance().getJsonObjectFromGet(subredditUrl);
        } catch (NetException e) {
            throw new DataSourceException("could not get JSON response from: " + subredditUrl, e);
        }
    }

    public JSONObject getComments(String permalink) throws DataSourceException {
        String permalinkUrl = URI_BASE + permalink + ".json";
        try {
            return HttpHelper.getInstance().getJsonArrayFromGet(permalinkUrl).getJSONObject(1);
        } catch (NetException e) {
            throw new DataSourceException("could not get JSON response from: " + permalinkUrl, e);
        } catch (JSONException e) {
            throw new DataSourceException("Could not parse JSON for: " + permalinkUrl, e);
        }
    }

    public JSONObject getSubreddit(String subreddit) {
        // TODO Auto-generated method stub
        return null;
    }

    public JSONObject getDefaultSubreddits() throws DataSourceException {
        try {
            return HttpHelper.getInstance().getJsonObjectFromGet(URI_SUBREDDITS);
        } catch (NetException e) {
            throw new DataSourceException("could not get JSON response from: " + URI_SUBREDDITS, e);
        }
    }

    public JSONObject getLinksForFrontPage() throws DataSourceException {
        String frontPageUrl = URI_BASE + "/.json";
        try {
            return HttpHelper.getInstance().getJsonObjectFromGet(frontPageUrl);
        } catch (NetException e) {
            throw new DataSourceException("could not get JSON response from: " + frontPageUrl, e);

        }
    }
}




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