使用facebook fql,有时当我的页面上没有满足下面查询的帖子时,查询将返回一个空的消息字符串,在这种情况下如何避免异常。
码
String query = "SELECT message,timeline_visibility, created_time FROM stream WHERE source_id = 187050104663230 AND message AND strlen(attachment.fb_object_type) < 1 AND type != 56 AND type = 46 AND strpos(message, \"prayer time(s)\") < 0 AND strpos(message, \"White days\") < 0 AND strpos(message, \"Hadith of the Day:\") < 0 AND created_time > " + facebook_check_post_Unix_Time + "LIMIT 1";
try
{
List<JsonObject> queryResults = facebookClient.executeFqlQuery(query, JsonObject.class);
facebook_post = queryResults.get(0).getString("message");
facebook_post_visibility = queryResults.get(0).getString("timeline_visibility");
}
catch (FacebookException e){logger.warn("Unexpected error", e); logger.warn("Unexpected error", e);}
catch (Exception e){logger.warn("Unexpected error", e); logger.warn("Unexpected error", e);}
错误讯息:
2013-12-22 07:46:35,913 WARN [javafxapplication4.JavaFXApplication4] - Unexpected error
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:638)
at java.util.ArrayList.get(ArrayList.java:414)
at java.util.Collections$UnmodifiableList.get(Collections.java:1369)
at javafxapplication4.JavaFXApplication4$1.run(JavaFXApplication4.java:1121)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)
答案 0 :(得分:0)
使用
if(!queryResults.isEmpty()) {
JsonObject facebookPost = queryResults.get(0);
String message = facebookPost.getString("message");
if(null != message && !"".equals(message)) {
// Do whatever you're trying to do with the message here
}
// Follow the same pattern for timeline_visibility
}
你得到了异常,因为你盲目地试图在一个你甚至不知道存在的对象上调用一个方法:
facebook_post = queryResults.get(0).getString("message");