0

I'm trying to develop an android application, and for my purpose, I've to be able to search tweets based on the Hashtag the user searches. I'm using Twitter4j Library and I have also Consumer API key/Secret and tokens.

What I've tried so far:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BottomNavigationView navView = findViewById(R.id.nav_view);
        mTextMessage = findViewById(R.id.message);
        navView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);


        // Twitter 4j 25.May.2019 only for test right now
        ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
        configurationBuilder.setDebugEnabled(true)
                .setOAuthConsumerKey("ABC")
                .setOAuthConsumerSecret("ABC")
                .setOAuthAccessToken("ABC")
                .setOAuthAccessTokenSecret("ABC");

        TwitterFactory tf = new TwitterFactory(configurationBuilder.build());
        final Twitter twitter = tf.getInstance();
        Query query = new Query("twitter4j");
        try {
            QueryResult result = twitter.search(query);
            for (Status status : result.getTweets()) {
                System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText());
            }
        } catch (TwitterException te) {
            System.out.println(te.getMessage());
        }

I expect some JSON request from Twitter, but on my console it shows nothing.

1 Answers1

0

The problem wasn't with the code, or with Twitter4j library, the problem was with Network configuration, specifically with "NetworkOnMainThreadException".

To fix the issue see here: How do I fix android.os.NetworkOnMainThreadException?