Exemplo n.º 1
0
  /**
   * This function returns a "response" (me.json) JSON data containing info about the user. <br>
   *
   * @return JSON data containing info about the user
   */
  private JSONObject info() throws IOException, ParseException {
    if (cookie == null || modhash == null) {
      System.err.printf(
          "Please invoke the \"connect\" function before attempting to call any other API functions.");
      Runtime.getRuntime().exit(-1);
    }

    JSONObject jsonObject =
        Utils.get("", new URL("http://www.reddit.com/api/me.json"), getCookie());
    return (JSONObject) jsonObject.get("data");
  }
Exemplo n.º 2
0
  /**
   * This function returns a list of messages
   *
   * @param user The user
   * @return A list containing messages
   * @throws IOException If connection fails
   */
  public static List<Message> getMessages(User user, MessageType messageType, int limit)
      throws IOException {

    ArrayList<Message> messages = new ArrayList<Message>();

    String urlString = "http://www.reddit.com/message/";

    switch (messageType) {
      case UNREAD:
        urlString += "unread";
        break;
      case INBOX:
        urlString += "inbox";
        break;
      case SENT:
        urlString += "sent";
        break;
    }

    urlString += ".json";
    urlString += "?";

    urlString += "limit=" + limit;

    URL url = new URL(urlString);

    JSONObject jsonObject = (JSONObject) Utils.get(url, user);

    //
    // DEBUG
    //
    // System.out.println(Utils.getJSONDebugString(jsonObject));

    JSONObject data = (JSONObject) jsonObject.get("data");
    JSONArray children = (JSONArray) data.get("children");

    for (int i = 0; i < children.size(); i++) {
      JSONObject jsonData = (JSONObject) children.get(i);
      messages.add(new Message(jsonData));
    }

    return messages;
  }