如何使用 Twitter 4J 更新推文?

发布于 2024-11-28 05:18:51 字数 3083 浏览 1 评论 0原文

我正在使用 Twitter4J (2.1.0) 尝试更新推文。我无法弄清楚我的代码有什么问题。

我的问题特别是:

(a) 并非所有推文都成功发布。我经常收到-1 的错误代码。根据 Google 群组帖子...

当内部 http 组件无法连接到时,您会得到代码 -1 或从 API 读取。当 API 运行时,您也可能会得到代码 -1 由于 DNS 相关问题,无法从 JVM 访问。

奇怪的是,我似乎几乎每隔一秒就会收到这个帖子。为了解决这个问题,每当我收到 -1 错误代码时,我都会尝试再次更新。虽然我意识到这不是一个很好的解决方案。这修复了 95% 的问题

(b) 每当新推文与任何旧推文匹配时,我都会收到重复错误(错误代码 403)

即使出现错误代码 403重复内容现已过时(例如,发布“Hello There”,发布各种状态更新,然后再次发布“Hello There”,抛出 TwitterException,错误代码为 403)

我当前的代码...

我的代码位于 AsyncTask 中,而 AsyncTask 又位于服务(而不是活动)中。我已经包含了 Asynctask 代码和下面的另一种方法......

    class SendTwitAsyncTask extends AsyncTask<String, Void, Void> {

    @Override
    protected Void doInBackground(String... params) {
        String tokenTwit = params[0];
        String tokenSecretTwit = params[1];
        String strMessageBody = params[2];

        AccessToken aToken = new AccessToken(tokenTwit, tokenSecretTwit);

        // initialize Twitter4J
        Twitter twitter = new TwitterFactory().getInstance();
            twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
            twitter.setOAuthAccessToken(aToken);


        // create a tweet
        // strMessageBody varies
        String tweet = strMessageBody;




        boolean bool = twitter.isOAuthEnabled();

        if (twitter.isOAuthEnabled()) {
            GeoLocation geolocation = new GeoLocation(-33.91, 151.25);
            try {

                twitter.updateStatus(tweet, geolocation);
                showNotification("Twitter One" , TWIT_SUCCESS); 

            } catch (TwitterException te) {

                if (te.getStatusCode() == -1) {
                    //try again
                    try {
                        twitter.updateStatus(tweet, geolocation);

                        showNotification("Twitter Two ", TWIT_SUCCESS);
                    } 
                    catch (TwitterException tetwo) {
                        describeTwitException(tetwo.getStatusCode());
                    }       
                } //end if

                //else exception other than -1
                else {
                    describeTwitException(te.getStatusCode());
                } //end else

            }// end outer catch
        } //end if
        else {
            showNotification("Unable to authenticate" , TWIT_FAIL);
        }//
        return null;
    }



} //end class SendTwitAsyncTask

public void describeTwitException(int twitExceptionCode) {
    switch (twitExceptionCode) {

    case (-1):
        showNotification("Twitter (unable to connect)", TWIT_FAIL);
        break;
    case(403):
        showNotification("Twitter (duplicate tweet)", TWIT_FAIL);
        break;
    default:
        showNotification("Twitter", TWIT_FAIL);

    } //end switch
}  //end describeTwitException method 

I am using Twitter4J (2.1.0) to try to update tweets. I can't figure out what is wrong with my code.

In particular my problems are:

(a) Not all tweets post successfully. I often get the error code of -1. According to a google group post...

You get code -1 when the internal http component fails to connect to
or read from the API. You may also get code -1 when the API is
unreachable from the JVM due to the DNS related issues.

Strangely I seemed to be getting this pretty much every second post. To deal with this whenever I received the -1 error code I would try to update again. While I realise this is not a very good solution. this fixed the probem 95% of the time

(b) I get a duplication errors (error code 403) whenever the new tweet matches any old tweet

Error code 403 occurs even if the duplicate is now outdated (eg. post "Hello there", post a variety of status updates, then post "Hello there" again throws a TwitterException with error code 403)

My current code...

My code is in an AsyncTask which is in turn in a Service (rather than activity). I have included the Asynctask code and another method below....

    class SendTwitAsyncTask extends AsyncTask<String, Void, Void> {

    @Override
    protected Void doInBackground(String... params) {
        String tokenTwit = params[0];
        String tokenSecretTwit = params[1];
        String strMessageBody = params[2];

        AccessToken aToken = new AccessToken(tokenTwit, tokenSecretTwit);

        // initialize Twitter4J
        Twitter twitter = new TwitterFactory().getInstance();
            twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
            twitter.setOAuthAccessToken(aToken);


        // create a tweet
        // strMessageBody varies
        String tweet = strMessageBody;




        boolean bool = twitter.isOAuthEnabled();

        if (twitter.isOAuthEnabled()) {
            GeoLocation geolocation = new GeoLocation(-33.91, 151.25);
            try {

                twitter.updateStatus(tweet, geolocation);
                showNotification("Twitter One" , TWIT_SUCCESS); 

            } catch (TwitterException te) {

                if (te.getStatusCode() == -1) {
                    //try again
                    try {
                        twitter.updateStatus(tweet, geolocation);

                        showNotification("Twitter Two ", TWIT_SUCCESS);
                    } 
                    catch (TwitterException tetwo) {
                        describeTwitException(tetwo.getStatusCode());
                    }       
                } //end if

                //else exception other than -1
                else {
                    describeTwitException(te.getStatusCode());
                } //end else

            }// end outer catch
        } //end if
        else {
            showNotification("Unable to authenticate" , TWIT_FAIL);
        }//
        return null;
    }



} //end class SendTwitAsyncTask

public void describeTwitException(int twitExceptionCode) {
    switch (twitExceptionCode) {

    case (-1):
        showNotification("Twitter (unable to connect)", TWIT_FAIL);
        break;
    case(403):
        showNotification("Twitter (duplicate tweet)", TWIT_FAIL);
        break;
    default:
        showNotification("Twitter", TWIT_FAIL);

    } //end switch
}  //end describeTwitException method 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿挽旧人 2024-12-05 05:18:51

Twitter API 将拒绝任何与您已发布的推文相匹配的推文。我认为旧推文永远不会“过期”。

The twitter API will reject any tweet that matches a tweet that you've already made. I don't think the old tweet ever 'expires.'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文