Making REST API calls from an Android app using TwitterApiClient class

痴心易碎 提交于 2019-12-19 04:38:07

问题


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 ?


回答1:


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


来源:https://stackoverflow.com/questions/27190313/making-rest-api-calls-from-an-android-app-using-twitterapiclient-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!