简体   繁体   中英

Get list of tweets in user time line using Fabric Android

I use the following code:

UserTimeline userTimeline = new UserTimeline.Builder().screenName("ZainAlabdin878").build();
final TweetTimelineListAdapter adapter = new TweetTimelineListAdapter(MainActivity.this, userTimeline);
System.out.println(adapter.getCount()+"");

I get the output 0 although I have tweets.

Am I doing something wrong? what I am trying to achieve is to get a list of tweets of a certain user. I'm using android studio and plugin.

*my goal is not to display the list but rather to get a List

many thanks.

final TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
    Fabric.with(context, new Twitter(authConfig), new TweetUi());

    TwitterCore.getInstance().logInGuest(new Callback<AppSession>() {
        @Override
        public void success(Result<AppSession> result) {
            AppSession session = result.data;
            TwitterApiClient twitterApiClient = TwitterCore.getInstance().getApiClient(session);
            twitterApiClient.getStatusesService().userTimeline(tweetId, screenName, tweetCount, null, null, false, false, false, true, new Callback<List<Tweet>>() {
                @Override
                public void success(Result<List<Tweet>> listResult) {
                    for (Tweet tweet : listResult.data) {
                     // here you will get list
                    }
                }

                @Override
                public void failure(TwitterException e) {
                    e.printStackTrace();
                }
            });
        }

        @Override
        public void failure(TwitterException e) {
            Log.e(TAG, "Load Tweet failure", e);
        }
    });

more details available here

Here's the code you'll need to actually fetch the Tweet objects:

ArrayList<Tweet> tweets = new ArrayList<>();
TwitterCore.getInstance().getApiClient(session).getStatusesService()
    .userTimeline(null,
            "screenname",
            10 //the number of tweets we want to fetch,
            null,
            null,
            null,
            null,
            null,
            null,
            new Callback<List<Tweet>>() {
                @Override
                public void success(Result<List<Tweet>> result) {
                    for (Tweet t : result.data) {
                        tweets.add(t);
                        android.util.Log.d("twittercommunity", "tweet is " + t.text);
                    }
                }

                @Override
                public void failure(TwitterException exception) {
                    android.util.Log.d("twittercommunity", "exception " + exception);
                }
            });

You'll need to run this in a new Thread (not the UI Thread)

EDIT

*Technically you don't have to run this in a new thread because it's asynchronous (someone please correct me here if I'm wrong). I know other Twitter API calls are async, so I'm assuming this one is too, although I cannot find where the actual call is happening in the Fabric source..*

Here's the full list of parameters for userTimeline() from the Fabric SDK

void userTimeline(@Query("user_id") Long var1, @Query("screen_name") String var2, @Query("count") Integer var3, @Query("since_id") Long var4, @Query("max_id") Long var5, @Query("trim_user") Boolean var6, @Query("exclude_replies") Boolean var7, @Query("contributor_details") Boolean var8, @Query("include_rts") Boolean var9, Callback<List<Tweet>> var10);

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