2

如何获取 Google+ 上帖子的评论?

我已经得到了帖子的内容和回复的数量。

public class Sample {

  private static Plus unauthenticatedPlus;

  public static void main(String[] args) throws IOException {

    try {
      setupTransport();

      getActivity();
    } catch (HttpResponseException e) {
      /* ... */
    }
  }

  /**
   * Setup the transport for our API calls.
   * @throws java.io.IOException when the transport cannot be created
   */
   private static void setupTransport() throws IOException {
       // Here's an example of an unauthenticated Plus object. In cases where you
       // do not need to use the /me/ path segment to discover the current user's
       // ID, you can skip the OAuth flow with this code.
       unauthenticatedPlus = Plus.builder(Util.TRANSPORT, Util.JSON_FACTORY)
           // When we do not specify access tokens, we must specify our API key instead
           // We do this using a JsonHttpRequestInitializer
           .setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() {
               @Override
               public void initialize(JsonHttpRequest jsonHttpRequest) throws IOException {
                 PlusRequest plusRequest = (PlusRequest) jsonHttpRequest;
                 plusRequest.setKey(Auth.GOOGLE_API_KEY);
               }
       }).build();

      /* Authorization part stripped, as this is not needed in this sample */
      /* ... */
  }

  /**
   * Get the most recent activity for the authenticated user.
   *
   * @throws IOException if unable to call API
   */
  private static void getActivity() throws IOException {
    // A known public activity ID
    String activityId = "z12gtjhq3qn2xxl2o224exwiqruvtda0i";

    /* Get an explicit public activity by ID */
    // We do not need to be authenticated to fetch this activity
    try {
      Activity activity = unauthenticatedPlus.activities().get(activityId).execute();
      show(activity);
    } catch (HttpResponseException e) {
      /* ... */
    }
  }

  /**
   * Print the specified activity on the command line.
   *
   * @param activity the activity to show
   */
  private static void show(Activity activity) {
    System.out.println("id: " + activity.getId());
    System.out.println("url: " + activity.getUrl());
    System.out.println("content: " + activity.getPlusObject().getContent());
    System.out.println("No of replies: " + activity.getPlusObject().getReplies().getTotalItems());
  }
}

提前致谢。

4

1 回答 1

3

获取活动评论需要向 Google+ API发出额外请求。

创建一个新PlusRequest实例

Plus.Comments.List listComments = plus.comments().list(activityId);

执行请求并将其解析为CommentFeed

CommentFeed commentFeed = listComments.execute();

获取Comment实例列表:

List<Comment> comments = commentFeed.getItems();
于 2012-09-11T06:38:22.070 回答