0

我正在按照官方文档( https://github.com/twitter/twitter-kit-android/wiki)在我的应用程序中实现 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");
            }
        };
4

1 回答 1

2

您可以在

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();
    }
}

twitter以不太方便createdAt的格式返回,但我们可以解析它。Wed Aug 27 13:08:45 +0000 2008

我建议您对其进行一些重构,将日历和解析逻辑提取到单独的函数中,但是您可以从上面的代码中得到这个想法。

于 2018-03-30T12:39:27.573 回答