我从 Twitter 检索数据。我已将一些 ID 存储在数据库中,并尝试使用 twitter API 检索信息。我正在使用以下代码:
if(cursor.hasNext()){
try {
while (cursor.hasNext()) {
final DBObject result = cursor.next();
JSONObject features = new JSONObject();
//System.out.println(result);
Map<String, Object> value = (Map<String, Object>) result.get("user");
boole.add((Boolean) value.get("default_profile"));
boole.add((Boolean) value.get("default_profile_image"));
features.put("_id", value.get("id"));
...
}
catch (JSONException e) {
System.err.println("JSONException while retrieving users from db: " + e);
} catch (TwitterException e) {
// do not throw if user has protected tweets, or if they deleted their account
if (e.getStatusCode() == HttpResponseCode.UNAUTHORIZED || e.getStatusCode() == HttpResponseCode.NOT_FOUND) {
} else {
throw e;
}
}
我添加了 twitter 异常,因为由于身份验证问题,我无法从某些用户检索数据。但是,当我的代码到达 catch (TwitterException e) 时,它会自动停止运行。我想继续到下一个光标(下一个数据库的id)
最佳答案
这是因为你的循环周围有 try catch block ,所以当捕获异常时,它就会脱离循环。
将 try catch block 放在 while block 内即可解决问题。
关于java - Twitter api 身份验证的 Try-catch 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23512580/