【问题标题】:Facebook4j: How to get total number of likes, comments and shares on a facebook post?Facebook4j:如何获取 Facebook 帖子上的点赞、评论和分享总数?
【发布时间】:2016-06-08 12:15:27
【问题描述】:

我正在尝试使用 facebook4j 检索有关 facebook 帖子的所有基本信息。

这是我的代码:

    final Reading reading1 = new Reading().since("-1 month").limit(38).fields("comments", "likes", "id", "message", "caption", "description", "created_time", "from").summary(); 
    final ResponseList<Post> feeds1 = facebook.getPosts("viratkohli", reading1);

    final Reading reading2 = new Reading().since("-1 month").fields("likes", "id", "message", "created_time", "from", "story").limit(100).summary(); 
    final ResponseList<Post> feeds2 = facebook.getPosts("viratkohli", reading2);



    for (int i = 0; i < feeds2.size(); i++) {
        // Get post.
        Post post1 = feeds2.get(i);
        // Get (string) message.
        String message = post1.getMessage();
        Date d = post1.getCreatedTime();
        String id = post1.getId();
       PagableList<Like> pl = post1.getLikes();
       String story = post1.getStory();

       System.out.println("post: "+i+": "+message);
       System.out.println("Created time: "+d.toString());
       System.out.println("id: "+id);
       System.out.println("story: "+story);
       System.out.println();
       for(Like like: pl) {
           String like_name = like.getName();
           String like_id = like.getId();

           System.out.println("like name: "+like_name);
           System.out.println("like id: "+like_id);
           System.out.println();
       }
       System.out.println("******************");

    }

        for(int j=0;j<feeds1.size();j++){
            Post post2 = feeds1.get(j);
            System.out.println(j);
            final PagableList<Comment> comments = post2.getComments();
            for(int k=0; k<comments.size(); k++) {
                // Get comment.
                Comment comment = comments.get(k);
                String id2 = comment.getId();
                String comm = comment.getMessage();
                Date d2 = comment.getCreatedTime();
                Integer co = comment.getLikeCount();

                System.out.println("Comment "+k+": "+comm);
                System.out.println("id: "+id2);
                System.out.println("created time: "+d2);

                }

            System.out.println("~~~~~~~~~~~~~");
            }

目前,我可以获得以下信息:

最大。 100 个最近的帖子,25 个最近的赞,每个帖子的 25 个最近的 cmets(最多 40 个帖子)。

我们将不胜感激任何有关克服限制问题的帮助。

【问题讨论】:

    标签: facebook facebook4j


    【解决方案1】:

    如果您只想要总点赞数,而不是单个点赞数 - 请将 limit 设置为 0,将 summary 设置为 1。

    https://developers.facebook.com/docs/graph-api/reference/object/likes#read

    【讨论】:

    • 我按照你说的做了。当您将限制设置为 0 时,您将一无所获。所以我没有使用限制属性(默认情况下,它设置为 25)。我调用了 shareCount() 方法。我得到了正确的计数值。问题在于喜欢和评论计数。查看响应 JSON 数组中的这些值:fields=likes,shares&summary=true&limit=25 shareCount=105, likes=PagableListImpl{count=null}。喜欢计数值为空。你能帮我获得正确的价值吗?如果我错过了什么,你可以告诉我。提前谢谢你。
    • 当您将摘要设置为 1 时,您当然会获得限制为 0 的数据(当然是一个,而不是提要本身):developers.facebook.com/tools/…
    • likes 和 cmets 在帖子的 Json 数组中具有单独的 Json 数组,其中的 count 值为 null,summary 为 null。它只显示喜欢或评论它的有限数量的人(身份证和人名)。你能分享一下获取计数的代码sn-p吗?它可以更好地帮助我。谢谢。
    • 检查我之前评论中的链接 - Graph API Explorer 中的实时示例显示了它是如何工作的。
    • 是的。我看到了。这是一个查询。但我需要这样做的编程方式。我通过检查数组并进行一些更改来做到这一点。现在我开始计算了。非常感谢您的帮助。
    【解决方案2】:

    这是正确的答案:

        final Reading reading = new Reading().summary().fields("id", "message", "likes.summary(true)", "shares", "comments.summary(true)").limit(20).since("-1 month"); 
        final ResponseList<Post> feeds = facebook.getPosts("green", reading);
    
        for (int i = 0; i < feeds.size(); i++) {
            // Get post.
            Post post = feeds.get(i);
    
           Integer likesCount = post.getLikes().getSummary().getTotalCount();
            Integer sharesCount = post.getSharesCount();
           Integer commentsCount = post.getComments().getSummary().getTotalCount();
    
           if(sharesCount == null)
               sharesCount = 0;
           if(likesCount == null)
               likesCount = 0;
           if(CommentsCount == null)
               commentsCount = 0;
            System.out.println("No. of shares for post "+i+": "+sharesCount);
            System.out.println("No. of likes for post "+i+": "+likesCount);
            System.out.println("No. of comments for post"+i+": "+commentsCount);
            System.out.println();
    

    【讨论】:

      猜你喜欢
      • 2012-11-04
      • 2016-08-15
      • 2015-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多