On each response you can call getRateLimitStatus()
to get a RateLimitStatus
. RateLimitStats.getRemaining()
will tell you the remaining number of API requests available for that family of calls, if that reaches zero you could then call RateLimitStatus.getSecondsUntilReset()
, and wait at least that long before making additional calls.
Information on Twitters rate limits can be found here: https://dev.twitter.com/docs/rate-limiting/1.1
Here is a basic example:
do {
TwitterResponse response = twitter.getFollowersIDs(userId, cursor);
RateLimitStatus status = response.getRateLimitStatus();
if(status.getRemaining() == 0) {
try {
Thread.sleep(status.getSecondsUntilReset() * 1000);
}
catch(InterruptedException e) {
// ...
}
}
} while(cursor > 0);
In the code you have now provided you are making 2 calls to Twitter, showUser
and getUserTimeLine
. You need to check the rate limit status after both of these calls (both User
and ResponseList
extend TwitterResponse
and have the rate limit information). These calls belong to 2 different resource families (users
and statuses
), both of these methods are allowed to be called 180 times per rate limit window (15 minutes).