javaandroidtwittertwitter-fabrictweets

Making REST API calls from an Android app using TwitterApiClient class


I'm using Twitter's Fabric SDK in my Android app. I need to acquire a Twitter user's Tweets & Status Messages. I have not been able to find any examples, and the documentation is not very clear on this, so I have posted a new question. Can someone provide an example of how to use the TwitterApiClient class ?


Solution

  • Twitter Kit has the ability to make API calls. The official documentation is here: https://dev.twitter.com/twitter-kit/android/api

    Everything starts with Statuses Service:

    StatusService service = Twitter.getApiClient().getStatusesService()
    

    Some methods available at Statuses Service (these methods have direct mapping to REST API endpoints, including the parameters.

    service.homeTimeline();
    service.lookup();
    service.mentionsTimeline();
    service.show();
    

    The mechanism to do a request is similar to all services, here's an example of Searching from Cannonball sample app code:

    final SearchService service = Twitter.getApiClient().getSearchService();
    
    service.tweets(SEARCH_QUERY, null, null, null, SEARCH_RESULT_TYPE, SEARCH_COUNT, null, null,
                maxId, true, new Callback<Search>() {
                    @Override
                    public void success(Result<Search> searchResult) {
                        Crashlytics.setLong(App.CRASHLYTICS_KEY_SEARCH_COUNT,
                                searchResult.data.searchMetadata.count);
                        setProgressBarIndeterminateVisibility(false);
                        final List<Tweet> tweets = searchResult.data.tweets;
                        adapter.getTweets().addAll(tweets);
                        adapter.notifyDataSetChanged();
                        if (tweets.size() > 0) {
                            maxId = tweets.get(tweets.size() - 1).id - 1;
                        } else {
                            endOfSearchResults = true;
                        }
                        flagLoading = false;
                    }
    
                    @Override
                    public void failure(TwitterException error) {
                        Crashlytics.logException(error);
    
                        setProgressBarIndeterminateVisibility(false);
                        Toast.makeText(PoemPopularActivity.this,
                                getResources().getString(R.string.toast_retrieve_tweets_error),
                                Toast.LENGTH_SHORT).show();
    
                        flagLoading = false;
                    }
                }
    );