Hello, Everyone.
In this blog, I am going to tell you how to use twitter fabric to get tweets in your application. But before going ahead, I am assuming that you have successfully installed fabric in your Android studio and if not, please read my previous blog by clicking the link below.
http://findnerd.com/account/#url=/list/view/Installing-twitter-fabric-to-your-android-studio/21374/
Now coming back to this,
- Click on the fabric icon and you will see a window on right side of your current activity.
MainActivity.java
TwitterCore.getInstance().logInGuest(new Callback<AppSession>() {
@Override
public void success(Result<AppSession> result) {
AppSession session = result.data;
Log.v("session is", session.getAuthToken().getAccessToken());
TwitterApiClient twitterApiClient = TwitterCore.getInstance().getApiClient(session);
SearchService searchService = twitterApiClient.getSearchService();
searchService.tweets("RRobertsWeather", null, null, null, null, 10, null, null, null, null, new Callback<Search>() {
@Override
public void success(Result<Search> result) {
Log.v("data is", result.data.tweets.get(9).text);
Log.v("result is", result.data.tweets.get(9).idStr);
}
@Override
public void failure(TwitterException exception) {
Log.v("failure is", String.valueOf(exception));
}
});
}
@Override
public void failure(TwitterException exception) {
Log.v("failure getting session", exception + "");
}
});
Here loginGuest method of twitter is used because it always maintains a session between Twitter and User.Remember to always use this LoginGuest method and then call tweet method inside the success of loginGuest method.If you don't use loginGuest method,your method of getting tweets will work for some particular time period and then it will show Error during fetching tweets.
Under the success callback of tweets method,you will see all the tweets of “Fabric”and in place of “10”,put your int value of how much tweets you want to see in a particular query.
That's all .Hope it helps :)
1 Comment(s)