Search code examples
androidandroid-studiotwitter-fabricandroid-twitter

Android How to get the public tweets with out making user login in fabrics


I know this question is asked already . I have read the twitter fabrics sdk. and its Auth but I am so dumb in understanding it. So below is the key point what I want

What I want

  1. I want to get all the tweets going on the basis of hash tag following is the example of what I want on mobile Tweets on #Wwe

  2. I want that my user should not have to sign in his/her twitter account. He just see the list of tweets in my app and get him self updated.

I have followed many tutorials but they are not helping me and also read how to get tweets from here and some other link .

Please tell me which auth do I need ? and How to do all these stuff. And good link with out twitter login please??


Solution

  • First you need to initialize Fabric with the key and secret of your twitter app:

    public void initFabric(Context context){
        if(!Fabric.isInitialized()) {       
              TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
              Fabric.with(context, new Twitter(authConfig));                 
        }else{
              Log.d(TAG, "Fabric already initialized");
        }
    }
    

    Just rememeber to keep your key and secret secret.

    Then you can retrieve the tweets by account and hashtag in the following way:

    public static TweetTimelineListAdapter retrieveTimeLineByHashtag(Context context, String hashtag){
        Log.d(TAG, "Loading tweets with hashtag " + hashtag);
        SearchTimeline searchTimeline = new SearchTimeline.Builder().query(hashtag).build();
        return new TweetTimelineListAdapter.Builder(context).setTimeline(searchTimeline).build();
    }
    
    public static TweetTimelineListAdapter retrieveTimeLineByAccount(Context context, String account){
        Log.d(TAG, "Loading tweets from user " + account);
        UserTimeline userTimeline = new UserTimeline.Builder().screenName(account).build();
        return new TweetTimelineListAdapter.Builder(context).setTimeline(userTimeline).build();
    }
    

    Also take in account that the SearchTimeline only search for recent tweets that includes your search words, so you can find that no tweets are returned. However, I couldn't find a better way to get the tweets by Hashtag using Fabric.

    Edit:

    public class Prueba extends AppCompatActivity {
    
        private static final String TAG = "TWITTER_PRUEBA";
        private ListView listView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_prueba);
    
            bindUI();
            initFabric(getApplicationContext());
        }
    
        @Override
        protected void onPostCreate(Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
    
            TweetTimelineListAdapter adapter = retrieveTimeLineByHashtag(this, "#Wwe");
            listView.setAdapter(adapter);
        }
    
        private void bindUI(){
            listView = (ListView) findViewById(R.id.prueba);
        }
    
        public static void initFabric(Context context){
    
            Log.d(TAG, "Initializing Fabric");
    
            if(!Fabric.isInitialized()) {
    
    
                    TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
                    Fabric.with(context, new Twitter(authConfig));
    
    
            }else{
                Log.d(TAG, "Fabric already initialized");
            }
        }
    
        public static TweetTimelineListAdapter retrieveTimeLineByHashtag(Context context, String hashtag){
            Log.d(TAG, "Loading tweets with hashtag " + hashtag);
            SearchTimeline searchTimeline = new SearchTimeline.Builder().query(hashtag).build();
            return new TweetTimelineListAdapter.Builder(context).setTimeline(searchTimeline).build();
        }
    
        public static TweetTimelineListAdapter retrieveTimeLineByAccount(Context context, String account){
            Log.d(TAG, "Loading tweets from user " + account);
            UserTimeline userTimeline = new UserTimeline.Builder().screenName(account).build();
            return new TweetTimelineListAdapter.Builder(context).setTimeline(userTimeline).build();
        }
    }
    

    Result:

    enter image description here