使用Facebook4J获取每个帖子的反应

时间:2017-04-13 06:21:06

标签: facebook4j

您是否尝试使用Facebook4J API对每篇文章做出反应。我一直在努力,但没有运气。

1 个答案:

答案 0 :(得分:0)

如果您想单独获取每个反应,并且对于每个帖子,您可以使用原始API调用并将其作为JSONObject获取。

private int like;
private int wow;
private int sad;
private int love;
private int haha;
private int angry;
private String postId;

Facebook facebook = new FacebookFactory().getInstance();
facebook.getOAuthAppAccessToken();

ResponseList<Post> posts = facebook.getFeed(pageName, reading);
        String pageId = facebook.getPage(pageName).getId();
        Paging<Post> paging = posts.getPaging();
        while(facebook.fetchNext(paging) != null) {
            paging = posts.getPaging();
            for(int i = 0; i < posts.size(); i++){
                this.postId = posts.get(i).getId();
                RawAPIResponse res = facebook.callGetAPI(postId+"?fields=reactions.type(LIKE).limit(0).summary(1).as(like),reactions.type(WOW).limit(0).summary(1).as(wow),reactions.type(SAD).limit(0).summary(1).as(sad),reactions.type(LOVE).limit(0).summary(1).as(love),reactions.type(HAHA).limit(0).summary(1).as(haha),reactions.type(ANGRY).limit(0).summary(1).as(angry)");
                JSONObject jsonObject = res.asJSONObject();
                JSONObject reaction = (JSONObject) jsonObject.get("like");
                JSONObject summary = (JSONObject) reaction.get("summary");
                    this.like = summary.getInt("total_count");
                        reaction = (JSONObject) jsonObject.get("wow");
                        summary = (JSONObject) reaction.get("summary");
                    this.wow = summary.getInt("total_count");
                        reaction = (JSONObject) jsonObject.get("sad");
                        summary = (JSONObject) reaction.get("summary");
                    this.sad = summary.getInt("total_count");
                        reaction = (JSONObject) jsonObject.get("love");
                        summary = (JSONObject) reaction.get("summary");
                    this.love = summary.getInt("total_count");
                        reaction = (JSONObject) jsonObject.get("haha");
                        summary = (JSONObject) reaction.get("summary");
                    this.haha = summary.getInt("total_count");
                        reaction = (JSONObject) jsonObject.get("angry");
                        summary = (JSONObject) reaction.get("summary");
                    this.angry = summary.getInt("total_count");

               }
             if(paging != null) {
                  posts = facebook.fetchNext(paging);
             }