facebook4j graph api将帖子的评论数量限制为25

时间:2014-12-01 13:54:47

标签: java facebook-graph-api paging

我正在尝试在粉丝页面示例“narendramodi”上获取针对特定帖子发布的所有评论 但是一些facebook图形API如何将注释数量限制为25,因为我的应用程序崩溃了“线程中的异常”主要“java.lang.IndexOutOfBoundsException:Index:25,Size:25”

我正在使用paging和fetchNext,但它仍无效。

这是我的代码 -

public class Facebooktest { 

static Facebook facebook;

        public static void main(String[] args) throws FacebookException, IOException {
        // Make the configuration builder
        ConfigurationBuilder confBuilder = new ConfigurationBuilder(); 
        confBuilder.setDebugEnabled(true);

        // Set application id, secret key and access token 
        confBuilder.setOAuthAppId("MyID"); 
        confBuilder.setOAuthAppSecret("MyAppSecret"); 

        confBuilder.setOAuthAccessToken("MyAccessToken");

        // Set permission 
        confBuilder.setOAuthPermissions("email,publish_stream, id, name, first_name, last_name, generic"); 
        confBuilder.setUseSSL(true); 
        confBuilder.setJSONStoreEnabled(true);

        // Create configuration object 
        Configuration configuration = confBuilder.build();

        // Create facebook instance 
        FacebookFactory ff = new FacebookFactory(configuration); 
        facebook = ff.getInstance();

        ResponseList<Post> results = facebook.getPosts("narendramodi");

        //facebook.getFriends(new Reading().fields("gender"));
        /*System.out.println("success");
        System.out.println(facebook.getMe().getFirstName());*/      

        String userId="";

        for (Post post : results) {
            System.out.println(post.getMessage() +"===="+  + post.getSharesCount() +"====="+ post.getLikes().size()); // Gives a max of 25 likes


           List<Comment> userComments =  getComments(post); 


            for (int j = 0; j < userComments.size(); j++) {

             userId=post.getComments().get(j).getFrom().getId();
             User user = facebook.getUser(userId);   

             System.out.println(user.getFirstName() + "---- "+ post.getComments().get(j).getLikeCount() +"---- "+ user.getHometown() + "======" + userComments.get(j).getMessage());

            }
        }

    }

    public static  List<Comment> getComments(Post post) {
        List<Comment> fullComments = new ArrayList<>();
        try {

            PagableList<Comment> comments = post.getComments();
            Paging<Comment> paging;
            do {
                for (Comment comment: comments)
                    fullComments.add(comment);


                paging = comments.getPaging();
            } while ((paging != null) && 
                    ((comments = facebook.fetchNext(paging)) != null));

        } catch (FacebookException ex) {
            Logger.getLogger(Facebook.class.getName())
                    .log(Level.SEVERE, null, ex);
        }

        return fullComments;
    }

}

另外如何获得页面上发布的主要“帖子”的总数。 ? 因为这部分代码(post.getLikes()。size())最多可以提供25个喜欢!

提前谢谢:)

3 个答案:

答案 0 :(得分:1)

默认情况下,返回的第一个项目数限制为25,最大数量为250.因此,您需要添加一个&#34;限制(250)&#34;请致电。

其次,您需要通过添加&#34;过滤器(流)&#34;来返回所有评论/喜欢。请求您的评论或喜欢。 Facebook&#34;隐藏&#34;一些评论或喜欢由于低故事&#34;值。

最后要获得总计数,您需要添加一个&#34;摘要(true)&#34;请求您的评论或喜欢。这将给出一个&#34;摘要&#34;密钥,&#34; total_count&#34;带有所有评论或喜欢总数的值的键。 (如果省略上面的过滤器参数,则只计算可见的过滤器参数)

有关详细信息,请参阅此处的评论。 Facebook Graph Api : Missing comments

答案 1 :(得分:0)

最后找到了解决方法。正如frank所建议的那样,我在URL中设置了限制并将数据作为json对象重新检索。

这是我的代码 -

   public class FacebookJson { 

    static Facebook facebook;
    public static void main(String[] args) throws FacebookException, IOException {
        // Make the configuration builder
        ConfigurationBuilder confBuilder = new ConfigurationBuilder(); 
        confBuilder.setDebugEnabled(true);

        // Set application id, secret key and access token 
        confBuilder.setOAuthAppId("MyAPPid"); 
        confBuilder.setOAuthAppSecret("MyAPPSecret"); 

        confBuilder.setOAuthAccessToken("AccessToken");
        //397879687034320|883YXTum1HBJMEbWKbsfq-80pV0
        // Set permission 
        confBuilder.setOAuthPermissions("email,publish_stream, id, name, first_name, last_name, generic"); 
        confBuilder.setUseSSL(true); 
        confBuilder.setJSONStoreEnabled(true);


        // Create configuration object 
        Configuration configuration = confBuilder.build();

        // Create facebook instance 
        FacebookFactory ff = new FacebookFactory(configuration); 
        facebook = ff.getInstance();

        ResponseList<Post> results1 = facebook.getPosts("narendramodi" , new Reading().limit(5));
         for (Post post : results1) {


        System.out.println(post.getMessage() +"===="+  + post.getSharesCount() + "====" + "====" + post.getId());           

      URL url = new URL("https://graph.facebook.com/{PostId}/comments?fields=parent,message,from,created_time,can_comment&filter=stream&limit=100?access_token={accesstoken}");
         try (InputStream is = url.openStream();
              JsonReader rdr = Json.createReader(is)) {

             JsonObject obj = (JsonObject) rdr.readObject();
             JsonArray results = (JsonArray) obj.get("data");
             for (JsonObject result : results.getValuesAs(JsonObject.class)) {
                 System.out.print(result.getJsonObject("from").getString("name"));
                 System.out.print(": ");
                 System.out.print(result.getJsonObject("from").getString("id"));
                 System.out.print(": ");
                 System.out.println(result.getString("message"));
                 System.out.println("-----------");
                 System.out.println(result.getString("id"));
                 System.out.println("-----------");
                 System.out.println(result.getString("created_time"));
             }


         }
        } 

    }

}

希望这有助于用户,但我仍然想知道如何在我的问题的代码中设置限制。 !如果有人可以提供帮助,将会很感激。

再次坦率地感谢你。 :)

答案 2 :(得分:0)

寻呼寻呼;                 做                 {

            for (Comment comment: comments)
                fullComments.add(comment);
            paging = comments.getPaging();
        }while((paging!=null)&&((feeds=facebook.fetchNext(paging))!=null));