0

我在commnent comment.getFrom().getId() 上调用getFrom() 时收到NullPointerException .... 抛出NullPointerException .....

我正在使用应用程序令牌.....

获取令牌

AccessToken accessToken = new DefaultFacebookClient(Version.VERSION_2_8).obtainAppAccessToken(
                        entry.getValue().getCredential().getAppId(),
                        entry.getValue().getCredential().getAppSecret());
                String token = accessToken.getAccessToken();

创建连接并获取页面提要和帖子

            Page page = facebookClient.fetchObject(getPageURL(), Page.class,
                    Parameter.with("since", sDate),
                    Parameter.with("until", uDate));

            Connection<Post> pageFeed = facebookClient.fetchConnection(page.getId() + "/feed", Post.class,
                    Parameter.with("limit", 100), Parameter.with("summary", 1),
                    Parameter.with("since",sDate),
                    Parameter.with("until",uDate));

从页面获取帖子详细信息

      Post postDetails = facebookClient.fetchObject(post.getId(), 
      Post.class, 
      Parameter.with("fields",
           "from,actions,message,story,to,likes.limit(0).summary(true),
           comments.limit(0).summary(true),shares.limit(0).summary(true)"));

获取 Post 的评论并调用 Comment.getFrom()

      Connection<Comment> comments = 
      facebookClient.fetchConnection(post.getId() + "/comments", 
      Comment.class,
                Parameter.with("limit", 100));
        boolean hasNext = true;
        while (hasNext && comments.getData().size() > 0) {
            for (Comment comment : comments.getData()) {
                currentCount++;
                currentCommentId=comment.getId();
                if (isValidUser(comment.getFrom().getId())  && (!isCommentsSearchEnabled() || filterCommentFeed(comment.getMessage()))) {
                    writeToSummaryFile(post.getId(), comment, currentCount);
                    getCommentsOfComment(comment.getId());
                }

            }

            if (comments.hasNext()) {
                comments = facebookClient.fetchConnectionPage(comments.getNextPageUrl(), Comment.class);
            } else
                hasNext = false;
        }
4

1 回答 1

0

您必须指定要获取的字段,因此请更改

facebookClient.fetchConnection(post.getId() + "/comments", 
  Comment.class,
            Parameter.with("limit", 100));

facebookClient.fetchConnection(post.getId() + "/comments", 
  Comment.class,
            Parameter.with("limit", 100), Parameter.with("fields","id,from");
于 2018-02-25T22:29:19.467 回答