简体   繁体   中英

Get tweets from user's timeline

I am trying to retrieve the text from every tweet of a user. I don't send a get request. I successfully connect with a tweeter account to my app with :

activity.client.authorize(activity, new Callback<TwitterSession>()
    {
        @Override
        public void success(Result<TwitterSession> result)
        {
            Toast.makeText(getApplicationContext(), "Login worked", Toast.LENGTH_LONG).show();
            writeInfile(result);
        }
        @Override
        public void failure(TwitterException e)
        {
            Toast.makeText(getApplicationContext(), "Login failed", Toast.LENGTH_LONG).show();
        }
    });

With the result I can access informations from my user such as his id or his name.

I also have the UserTimeLine

final UserTimeline userTimeline = new UserTimeline.Builder()
            .userId(result.data.getUserId())
            .build();

How can I get his tweets or the id of his tweets from there ?

Thank you

I managed to do it by looking at every methods given by auto completion so if it can help anyone, here is how I did it :

void writeData(ContentResolver contentResolver)
{
    activity.client.authorize(activity, new Callback<TwitterSession>()
    {
        @Override
        public void success(Result<TwitterSession> result)
        {
            writeInfile(result);
        }
        @Override
        public void failure(TwitterException e)
        {
            Log.e("Twitter Log in", e.toString());
        }
    });
}


void writeInFile(Result<TwitterSession> result)
{
    userTimeline = new UserTimeline.Builder()
                 .userId(result.data.getUserId())
                 .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;
        }
        if (searchResult.data.items.size() == 200)
            userTimeline.previous(maxId,  callback);
        else
            closeOutputFile();

    }
    @Override
    public void failure(TwitterException error)
    {
       //Do something
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM