Java 尝试捕获twitter api身份验证的问题
我从twitter上检索数据。我在数据库中存储了一些ID,我正在尝试使用twitter API检索信息。我正在使用以下代码:Java 尝试捕获twitter api身份验证的问题,java,exception,twitter,twitter4j,Java,Exception,Twitter,Twitter4j,我从twitter上检索数据。我在数据库中存储了一些ID,我正在尝试使用twitter API检索信息。我正在使用以下代码: if(cursor.hasNext()){ try { while (cursor.hasNext()) { final DBObject result = cursor.next(); JSONObject features = new JSONObject(); //
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;
}
}
if(cursor.hasNext()){
试试{
while(cursor.hasNext()){
最终DBObject结果=cursor.next();
JSONObject features=新的JSONObject();
//系统输出打印项次(结果);
Map value=(Map)result.get(“用户”);
boole.add((布尔)value.get(“默认_配置文件”);
boole.add((布尔)value.get(“默认配置文件\图像”);
features.put(“_id”,value.get(“id”);
...
}
捕获(JSONException e){
System.err.println(“从db检索用户时出现JSONException:”+e);
}捕获(twitter异常){
//如果用户有受保护的推文,或者他们删除了他们的帐户,请不要抛出
如果(e.getStatusCode()==HttpResponseCode.UNAUTHORIZED | | e.getStatusCode()==HttpResponseCode.NOT|u FOUND){
}否则{
投掷e;
}
}
我添加了twitter异常,因为由于身份验证问题,我无法从某些用户检索数据。但是,当我的代码达到catch(TwitterException e)时,它会自动停止运行。我想继续到下一个游标(下一个数据库的id)这是因为您在循环中使用了try-catch块,所以当捕获到异常时,它将退出循环
将try-catch块放在while块中可以解决问题。如果您想这样做,您需要将
try-catch
块移动到while
中。当前,一旦在while
中抛出异常,它将退出while
并转到catch
块,执行将在t后停止帽子。如果在while
中移动try
,则即使在引发和处理异常后,while
仍将继续
循环和try-catch的结构应该如下所示
while (cursor.hasNext()) {
try {
// The code
}
catch (JSONException e) {
// your code
}
catch (TwitterException e) {
// your code
}
}