I am implementing twitter kit for android in my application following the official documentation (https://github.com/twitter/twitter-kit-android/wiki). I make the login and I get the basic data correctly and without problem.
When I want to get the user's tweets, or time line, the way to do it is indicated but always shown in a list or recyclerview (https://github.com/twitter/twitter-kit-android/wiki/Show-Timelines)
I have seen these examples also in stackoverflow where the same solution is given, but always turning the data into a list or recyclerview
My question: is there any way to get just the JSON response to the query, ?
The answers I have found do not specifically respond to this.
In the following way it is possible to obtain a list of tweets, but I can not apply search filters like the date, or keywords (untilDate, etc)
void writeInFile()
{
userTimeline = new UserTimeline.Builder()
.userId(userID)
.includeRetweets(false)
.maxItemsPerRequest(200)
.build();
userTimeline.next(null, callback);
}
Callback<TimelineResult<Tweet>> callback = new Callback<TimelineResult<Tweet>>()
{
@Override
public void success(Result<TimelineResult<Tweet>> searchResult)
{
List<Tweet> tweets = searchResult.data.items;
for (Tweet tweet : tweets)
{
String str = tweet.text; //Here is the body
maxId = tweet.id;
Log.v(TAG,str);
}
if (searchResult.data.items.size() == 100) {
userTimeline.previous(maxId, callback);
}
else {
}
}
@Override
public void failure(TwitterException error)
{
Log.e(TAG,"Error");
}
};
You get all the necessary data in the
public void success(Result<TimelineResult<Tweet>> searchResult)
callback.
You have your list of tweets from
searchResult.data.items;
and then you can pick only the data you need. Tweet class has a lot of data inside that you can use. Here are the docs.
If you compare it with JSON api response you can see that you have the same info.
All you need to do is get data from your Tweet object and filter based on it. For example let's get only tweets that were created during last 6 hours:
List<Tweet> tweets = searchResult.data.items;
List<Tweet> filteredTweets = new ArrayList<>();
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.HOUR_OF_DAY, -6);
Date sixHoursBefore = cal.getTime();
for (Tweet tweet : tweets)
{
Date tweetCreatedAtDate = null;
try {
tweetCreatedAtDate = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy").parse(tweet.createdAt);
if (tweetCreatedAtDate.after(sixHoursBefore)) {
filteredTweets.add(tweet);
}
} catch (ParseException e) {
e.printStackTrace();
}
}
There twitter returns createdAt
in format Wed Aug 27 13:08:45 +0000 2008
which is not very handy, but we can parse it.
I'd recommend you to refactor it a bit, pull out calendar and parsing logic to a separate functions, but you can get the idea from the code above.