[英]facebook4j graph api limiting the no of comments to 25 on a post
I'm trying to fetch all the comments posted for a particular post on the fan page example "narendramodi" But some how the facebook graph API is limiting the number of comments to 25 , as my application crashes with "Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 25, Size: 25" 我正在尝试获取在风扇页面示例“ narendramodi”上针对特定帖子发布的所有评论,但由于我的应用程序因“线程“ main”中的异常”而崩溃,因此facebook graph API如何将评论数限制为25 java.lang.IndexOutOfBoundsException:索引:25,大小:25“
I'm using paging and fetchNext , but still it is not working. 我正在使用分页和fetchNext,但仍然无法正常工作。
Here is my code - 这是我的代码-
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;
}
}
Also How to get the total no of likes of the main "post" posted on the page. 以及如何获得页面上发布的主要“帖子”的喜欢总数。 ?
? As this part of the code (post.getLikes().size()) gives a max of 25 likes !
由于这部分代码(post.getLikes()。size())最多提供25个赞!
Thanks In advance :) 提前致谢 :)
First number of returned items is limited to 25 by default and maxed at 250. So you need to add a "limit(250)" param to your call. 默认情况下,返回项的首个数限制为25,最大为250。因此,您需要在调用中添加“ limit(250)”参数。
Second you need to return all comments/likes by adding a "filter(stream)" param to your call for comments or likes. 其次,您需要通过在评论或喜欢的调用中添加“ filter(stream)”参数来返回所有评论/喜欢。 Facebook "hides" some comments or likes due to a low "story" value.
Facebook由于“故事”值低而“隐藏”了一些评论或顶。
Finally to get a total count you need to add a "summary(true)" param to your call for comments or likes. 最后,要获得总数,您需要在通话中添加“ summary(true)”参数以进行评论或顶。 This will give to a "summary" key, with a "total_count" key with a value for the total count of all comments or likes.
这将提供一个“摘要”键,以及一个“ total_count”键,其中包含所有评论或喜欢的总数的值。 (If you omit the filter param above it will only count the visible ones)
(如果省略上面的过滤器参数,则只会计算可见的参数)
See my comments here for more detailed info. 在这里查看我的评论以获取更多详细信息。 Facebook Graph Api : Missing comments
Facebook Graph Api:缺少评论
Finally found a workaround to this. 终于找到了解决方法。 As suggested by frank I set the limit in the URL and retrived the data as a json object.
坦率地说,我在URL中设置了限制,并将数据作为json对象检索。
Here is my code - 这是我的代码-
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"));
}
}
}
}
}
Hope this helps the users , but still I want to know how to set the limit in the code of my question. 希望这对用户有帮助,但我仍然想知道如何在问题代码中设置限制。 !
! If anyone can help it will be grateful.
如果有人可以帮助,将不胜感激。
And thank you once again frank. 再次感谢您坦白。 :)
:)
Paging paging; 分页寻呼; do {
做{
for (Comment comment: comments)
fullComments.add(comment);
paging = comments.getPaging();
}while((paging!=null)&&((feeds=facebook.fetchNext(paging))!=null));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.