Twitter4j未处理的异常

时间:2016-03-19 06:24:44

标签: java eclipse twitter

Twitter.getHomeTimeline()返回来自用户家庭时间线的最新推文列表。不幸。 Eclipse告诉我,我有一个毫无瑕疵的例外。当我查找方法时,getHomeTimeline()告诉我,当twitter网络不可用时抛出异常。怎么会这样?我是这个twitter4j API的新手,并不了解目前无法访问网络。

package com.follow3d.rob;
import java.util.List;
import twitter4j.*;
import twitter4j.conf.*;
public class Follow3d {

public static void main(String[] args) {


    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true)
      .setOAuthConsumerKey("XXXXXXXXXX")//removed on purpose
      .setOAuthConsumerSecret("XXXXXXXXXX")//removed on purpose
      .setOAuthAccessToken("XXXXXXXXXX")//removed on purpose
      .setOAuthAccessTokenSecret("  XXXXXXXXXX");//removed on purpose
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();

    List<Status> statuses = twitter.getHomeTimeline();
    for (Status status1 : statuses ){
        System.out.println(status1.getUser().getName() + ": " + status1.getText());
        }
    }
}
  

抛出:   TwitterException - 当Twitter服务或网络不可用时

1 个答案:

答案 0 :(得分:0)

塔达!!!!有用!!!谢谢你的帮助。我只需要处理异常。

package com.follow3d.rob;

import java.util.List;
import twitter4j.*;
import twitter4j.conf.*;

public class Follow3d {

public static void main(String[] args) {

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey("xxxxxxxxxx")
            .setOAuthConsumerSecret("xxxxxxxxxx")
            .setOAuthAccessToken("xxxxxxxxxx")
            .setOAuthAccessTokenSecret("xxxxxxxxxx");
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();
    try {
        List<Status> statuses = twitter.getHomeTimeline();
        for (Status status1 : statuses) {
            System.out.println(status1.getUser().getName() + ": " + status1.getText());
        }
    } catch (TwitterException name) {
        System.out.println("You don't have internet connection.");
    }

    finally {
        System.out.println("You don't have internet connection.");
    }
  }
}