comment.getFrom().getId is Null restfb facebook comments



我在commnent上调用getfrom((时会获得nullpointerexceptioncomment.getFrom((。getID((....抛出NullPoInterException ....

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

获得令牌

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

创建连接并获取页面feed和porves

            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)"));

获取帖子和呼叫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;
        }

您必须指定要获取的字段,所以请更改

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

to

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

最新更新