连接拒绝与twitter4j

时间:2013-04-08 11:52:51

标签: java twitter twitter4j

我正在使用twitter4j-core-2.2.6.jar运行以下代码在Twitter上发推文。

package twitter4j.examples.tweets;
//import java.awt.List;

import java.util.List;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.OAuthAuthorization;
import twitter4j.conf.ConfigurationBuilder;

public class TwitterTest{
    private static final String ACCESS_TOKEN = ""; // proper access_token
    private static final String ACCESS_TOKEN_SECRET = ""; // proper secret_token
    private static final String CONSUMER_KEY = ""; // proper consumer_key
    private static final String CONSUMER_SECRET = ""; // proper consumer_secret

    public static void main(String[] args)
    {
        ConfigurationBuilder builder = new ConfigurationBuilder();
        builder.setOAuthAccessToken(ACCESS_TOKEN);
        builder.setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET);
        builder.setOAuthConsumerKey(CONSUMER_KEY);
        builder.setOAuthConsumerSecret(CONSUMER_SECRET);
        OAuthAuthorization auth = new OAuthAuthorization(builder.build());

       Twitter twitter = new TwitterFactory().getInstance(auth);
       /* List<Status> statuses = null;
        try {
            statuses = twitter.getHomeTimeline();
        } catch (TwitterException e1) {
            System.out.println("Error");
            e1.printStackTrace();
        }
        System.out.println("Showing home timeline.");
        for (Status status : statuses) {
            System.out.println(status.getUser().getName() + ":" +
                               status.getText());
        }*/
        try 
        {
            twitter.updateStatus("Hello World!");
            System.out.println("yessssss\n");
        } catch (TwitterException e) 
        {
            System.err.println("Error occurred while updating the status!="+e.getErrorMessage());
            e.printStackTrace();
            return;
        }
        System.out.println("Successfully updated the status.");
    }
}

当我将状态更新为非null值时,它给出了以下错误。

TwitterException{exceptionCode=[126cde36-60c4a990 126cde36-60c4a966], statusCode=-1, retryAfter=-1, rateLimitStatus=null, featureSpecificRateLimitStatus=null, version=2.2.6}
        at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:200)
        at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:65)
        at twitter4j.internal.http.HttpClientWrapper.post(HttpClientWrapper.java:102)
        at twitter4j.TwitterImpl.post(TwitterImpl.java:1762)
        at twitter4j.TwitterImpl.updateStatus(TwitterImpl.java:399)
        at twitter4j.examples.tweets.TwitterTest.main(TwitterTest.java:42)
    Caused by: java.net.ConnectException: Connection refused
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:327)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:193)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:180)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:384)
        at java.net.Socket.connect(Socket.java:546)
        at sun.net.NetworkClient.doConnect(NetworkClient.java:173)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:409)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:530)
        at sun.net.www.http.HttpClient.<init>(HttpClient.java:240)
        at sun.net.www.http.HttpClient.New(HttpClient.java:321)
        at sun.net.www.http.HttpClient.New(HttpClient.java:338)

我该如何解决这个问题?

关于拒绝连接的相关讨论可以在因特网上找到:

1 个答案:

答案 0 :(得分:0)

尝试更改

OAuthAuthorization auth = new OAuthAuthorization(builder.build());
Twitter twitter = new TwitterFactory().getInstance(auth);

TwitterFactory twitterFactory = new TwitterFactory(builder.build());
Twitter twitter = twitterFactory.getInstance();

那应该有用