我们正在尝试做的是创建一个机器人来检查某人是否是 twitch 频道的追随者。如果它们是或不是,我们返回一个布尔值。在这种情况下,我们调用 JSON 对象并使用 .get("error") 来查看它是否存在。如果存在,我们希望返回 false,因为这意味着他们不是追随者。如果找不到它应该抛出 JsonIOException,但它永远不会。如果他们确实遵循它会返回:http://pastebin.com/RxvTTZUA应该会中断,因为没有错误键。如果他们没有关注它,它会返回:http ://pastebin.com/Qha8UGat ,然后应该返回 false。这是检查它的代码:
public static boolean isFollower(String channel, String sender) {
try {
String nextUrl = "https://api.twitch.tv/kraken/users/"+sender+"/follows/channels/"+channel;
System.out.println(nextUrl);
JsonObject following = new JsonParser().parse(new JsonReader(new InputStreamReader(new URL(nextUrl).openStream()))).getAsJsonObject();
try {
following.get("error");
return false;
} catch (JsonIOException e) {
return true;
}
} catch (JsonIOException | JsonSyntaxException | IOException e) {
logger.log(Level.SEVERE, "An error occurred checking if "+sender+" is following "+channel.substring(1), e);
}
return false;
}
第 4 行中的 println 只是我自己试图找出问题所在,但我很困惑。