[英]get twitter user timeline and apply filters
我正在按照官方文档( https://github.com/twitter/twitter-kit-android/wiki )在我的应用程序中为 android 实现 twitter 套件。 我登录并正确获取基本数据,没有问题。
当我想获取用户的推文或时间线时,会指示执行此操作的方法,但始终显示在列表或回收站视图中( https://github.com/twitter/twitter-kit-android/wiki/Show-Timelines )
我也在 stackoverflow 中看到了这些示例,其中给出了相同的解决方案,但总是将数据转换为列表或 recyclerview
我的问题:有没有办法只获得对查询的 JSON 响应?
我找到的答案并没有具体回应这一点。
通过以下方式可以获得推文列表,但我不能应用搜索过滤器,如日期或关键字(直到日期等)
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");
}
};
您可以在
public void success(Result<TimelineResult<Tweet>> searchResult)
打回来。
你有你的推文列表
searchResult.data.items;
然后你可以只选择你需要的数据。 Tweet类里面有很多你可以使用的数据。 这是文档。
如果将其与JSON api 响应进行比较,您可以看到您拥有相同的信息。
您需要做的就是从您的 Tweet 对象中获取数据并基于它进行过滤。 例如,让我们只获取过去 6 小时内创建的推文:
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();
}
}
有叽叽喳喳返回createdAt
格式Wed Aug 27 13:08:45 +0000 2008
这是不是很方便,但我们可以分析它。
我建议您稍微重构一下,将日历和解析逻辑提取到单独的函数中,但您可以从上面的代码中获得想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.