Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Getting tweets using latest Twitter Rest API's

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 4
    • 0
    • 403
    Comment on it

    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.

     

    • Select your project in which you want to use twitter fabric.

     

    • Twitter toolkit will appear after selecting a project and here you have to choose kit named Twitter with twitter icon.

     

    • As soon as you click on Twitter kit,You will see a button written Install.

    •  

     

    • Click on Apply button and wait till the changes are getting done to your project.Main Activity of your project will have all the changes done.Don't panic,the changes are just fabric changes.No harm will be done to your written code :)

     

    • Once the changes are done,Run your project.Fabric will determine whether the changes are done correctly or not.

     

    • Once you see Done icon on fabric window,Minimize the fabric window.

     

    • Now go to the activity or fragment where you want to get tweets from twitter and copy the following code. In my case,activity is MainActivity.

    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 :)

    Getting tweets using latest Twitter Rest API's

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: