我正在尝试获取粉丝页面示例“narendramodi”上特定帖子发布的所有评论 但是 facebook graph API 如何将评论数量限制为 25 ,因为我的应用程序因“线程“main”中的异常 java.lang.IndexOutOfBoundsException: Index: 25, Size: 25”而崩溃
我正在使用分页和 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 个赞!
提前致谢:)
返回项目的第一个数量默认限制为 25,最大为 250。因此您需要在调用中添加“limit(250)”参数。
其次,您需要通过在您的评论或点赞调用中添加“过滤器(流)”参数来返回所有评论/点赞。由于“故事”价值较低,Facebook“隐藏”了一些评论或点赞。
最后,要获得总计数,您需要在您的评论或点赞调用中添加“summary(true)”参数。 这将给出一个“summary”键,以及一个“total_count”键,其值为所有评论或喜欢的总数。 (如果省略上面的过滤器参数,它将只计算可见的)
请参阅我的评论以获取更多详细信息。 Facebook Graph Api:缺少评论
终于找到了解决这个问题的方法。正如 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"));
}
}
}
}
}
希望这对用户有帮助,但我仍然想知道如何在我的问题代码中设置限制。 !如果有人可以帮忙,我将不胜感激。
再次感谢您坦白地说。 :)
寻呼寻呼; 做 {
for (Comment comment: comments)
fullComments.add(comment);
paging = comments.getPaging();
}while((paging!=null)&&((feeds=facebook.fetchNext(paging))!=null));