1

我尝试操作 twitter4j 的基本功能来训练我这个库。我的第一个测试是检索我的时间线,这是 twitter4j 网站上的第一个教程之一。

这是我检索时间线的代码:

String accessToken = "***";
String accessSecret = "***";
String consumerKey = "***";
String consumerSecret = "***";

ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
    .setOAuthConsumerKey(consumerKey)
    .setOAuthConsumerSecret(consumerSecret)
    .setOAuthAccessToken(accessToken)
    .setOAuthAccessTokenSecret(accessSecret);

TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();

List<Status> statuses = twitter.getHomeTimeline();

System.out.println("Showing home timeline.");

for (Status status : statuses) {

    System.out.println(status.getUser().getName() + ":" +
    status.getText());

}

但是使用这个简单的代码,我有以下错误:

Exception in thread "main" api.twitter.com
Relevant discussions can be found on the Internet at:
    http://www.google.co.jp/search?q=e5488403 or
    http://www.google.co.jp/search?q=09b41b1e
TwitterException{exceptionCode=[e5488403-09b41b1e 6ece33f2-2ccf4468], statusCode=-1, message=null, code=-1, retryAfter=-1, rateLimitStatus=null, version=3.0.3}
    at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:192)
    at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:61)
    at twitter4j.internal.http.HttpClientWrapper.get(HttpClientWrapper.java:81)
    at twitter4j.TwitterImpl.get(TwitterImpl.java:1835)
    at twitter4j.TwitterImpl.getHomeTimeline(TwitterImpl.java:117)
    at Test.main(Test.java:32)
Caused by: java.net.UnknownHostException: api.twitter.com
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at sun.net.NetworkClient.doConnect(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.<init>(Unknown Source)
    at sun.net.www.http.HttpClient.New(Unknown Source)
    at sun.net.www.http.HttpClient.New(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at java.net.HttpURLConnection.getResponseCode(Unknown Source)
    at twitter4j.internal.http.HttpResponseImpl.<init>(HttpResponseImpl.java:34)
    at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:156)
    ... 5 more

问题出在哪里 ?

4

1 回答 1

0

最后,我遇到了问题,代码很完美,但我有一个代理。所以我修复了它:

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true)
            .setOAuthConsumerKey(consumerKey)
            .setOAuthConsumerSecret(consumerSecret)
            .setOAuthAccessToken(accessToken)
            .setOAuthAccessTokenSecret(accessSecret)

            // PROXY
            .setHttpProxyHost(proxyHost)
            .setHttpProxyPort(proxyPort)
            ;
于 2013-05-17T12:48:55.890 回答