2

我正在使用 twitter4j 获取用户时间线。我正在使用 twitter4j-3.0.4-SNAPSHOT jars,我在代码下面提到了。我总是收到错误说明连接超时。请提供建议。

ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true)
                .setOAuthConsumerKey("ConsumerKey")
                .setOAuthConsumerSecret("ConsumerSecret")
                .setOAuthAccessToken("AccessToken")
                .setOAuthAccessTokenSecret("AccessTokenSecret");        

        Twitter twitter = new TwitterFactory(cb.build()).getInstance();
        try {
            List<Status> statuses;
            String user;
            if (args.length == 1) {
                user = args[0];
                statuses = twitter.getUserTimeline(user);
            } else {
                user = twitter.verifyCredentials().getScreenName();
                statuses = twitter.getUserTimeline();
            }
            System.out.println("Showing @" + user + "'s user timeline.");
            for (Status status : statuses) {
                System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
            }
        } catch (TwitterException te) {
            te.printStackTrace();
            System.out.println("Failed to get timeline: " + te.getMessage());
            System.exit(-1);
        }

错误信息

connect timed out
Relevant discussions can be found on the Internet at:
    http://www.google.co.jp/search?q=00df323f or
    http://www.google.co.jp/search?q=40b8353e
TwitterException{exceptionCode=[00df323f-40b8353e c60d4d36-d64e6e48], statusCode=-1, message=null, code=-1, retryAfter=-1, rateLimitStatus=null, version=3.0.4-SNAPSHOT(build: f34757f6d8512eca8028601d9de303e0173d8d42)}
    at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:177)
    at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:61)
    at twitter4j.internal.http.HttpClientWrapper.get(HttpClientWrapper.java:81)
    at twitter4j.TwitterImpl.get(TwitterImpl.java:1838)
    at twitter4j.TwitterImpl.getUserTimeline(TwitterImpl.java:156)
    at twitter4j.TwitterImpl.getUserTimeline(TwitterImpl.java:181)
    at com.tcs.twitter.GetTweets.main(GetTweets.java:56)
Caused by: java.net.SocketTimeoutException: connect timed out
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
    at java.net.Socket.connect(Socket.java:519)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:158)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
    at sun.net.www.http.HttpClient$4.run(HttpClient.java:457)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.net.www.http.HttpClient.privilegedOpenServer(HttpClient.java:439)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:520)
    at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:272)
    at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:329)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:172)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:831)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:158)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1041)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:373)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:318)
    at twitter4j.internal.http.HttpResponseImpl.<init>(HttpResponseImpl.java:34)
    at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:141)
    ... 6 more
Failed to get timeline: connect timed out
4

1 回答 1

0
ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true)
            .setOAuthConsumerKey("ConsumerKey")
            .setOAuthConsumerSecret("ConsumerSecret")
            .setOAuthAccessToken("AccessToken")
            .setOAuthAccessTokenSecret("AccessTokenSecret").setHttpConnectionTimeout(100000);  

我认为您的互联网连接速度很慢,因此您可以增加 httpConnectionTimeOut。http.connectionTimeout 的默认值为 20000 毫秒。详细信息可以在http://twitter4j.org/en/configuration.html找到。

于 2013-06-13T08:05:53.413 回答