我正在使用Twitter4J(2.1.0)尝试更新推文。我无法弄清楚我的代码有什么问题。如何使用Twitter 4J更新推文?
特别是我的问题是:
(一)并非所有的微博成功发布。我经常得到-1的错误代码。根据谷歌组post ...
你得到的代码-1当内部HTTP组件无法连接到 或从API读取。由于DNS相关问题,您无法从JVM获取API ,因此您也可能会得到代码-1。
奇怪的是,我似乎每秒钟都会收到这个帖子。为了处理这个问题,只要我收到-1错误代码,我会尝试再次更新。虽然我意识到这不是一个很好的解决方案。这个固定的probem的时间
(B)我得到一个重复的错误(错误代码403),每当新的鸣叫匹配任何旧鸣叫95%
错误代码403,即使重复发生现在已经过时(如发布“你好”,张贴各种状态更新,然后发布“你好”再次抛出,错误代码403 TwitterException)
我当前的代码...
我的代码位于AsyncTask中,而AsyncTask又位于Service(而不是活动)中。我已经包括的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
谢谢你的答案。我试着从Twitter的android应用程序发送重复推文。它不会让我发送最近在我的时间线上的重复推文。但是,无论原始推文是否很旧(例如,大于一年),重复的推文都会成功发布。 PS。投票你的答案,但留下问题打开,因为仍然不确定为什么我得到-1错误代码状态。 – Mel