skip to Main Content

I googled to get tweets of a person from Twitter API (JSON Object).

But i didn’t get any perfect solution to it.

I tried using QueryResult, but its showing either # HASH_TAG or @ Tag.

Is there any way to get tweets of a person (I have the User ID and Screen Name).

Here is the code I tried:

ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);

User user;
AccessToken accessToken = new AccessToken(TWITTER_ACCESS_TOKEN, TWITTER_ACCESS_TOKEN_SECRET);
Twitter twitter = new TwitterFactory(builder.build()).getInstance(accessToken);

List<twitter4j.Status> statusList = new ArrayList<>();
Query query = new Query("%23tarak9999");
query.setCount(20);

QueryResult result = twitter.search(query);
statusList = result.getTweets();

Thank You

2

Answers


  1. Chosen as BEST ANSWER

    The answer mentioned above by Malik Ahsan is throwing an error

    RunTimeError: Must initialize Twitter before using getInstance().

    Adding the below lines solved the issue:

    TwitterConfig config = new TwitterConfig.Builder(this)
                .twitterAuthConfig(new TwitterAuthConfig(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET)).build();
    Twitter.initialize(config);
    

    The above lines to be added above

    UserTimeline userTimeline = new UserTimeline.Builder().screenName("SCREEN_NAME").build();


  2. Here is an example Activity setting up a UserTimeline:

    import com.twitter.sdk.android.tweetui.TweetTimelineListAdapter;
    import com.twitter.sdk.android.tweetui.UserTimeline;
    public class TimelineActivity extends ListActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.timeline);
    TwitterConfig config = new TwitterConfig.Builder(this)
            .twitterAuthConfig(new
    TwitterAuthConfig(TWITTER_CONSUMER_KEY, 
    TWITTER_CONSUMER_SECRET)).build();
    Twitter.initialize(config);
    final UserTimeline userTimeline = new UserTimeline.Builder()
            .screenName("twitterdev")
            .build();
    final TweetTimelineListAdapter adapter = new TweetTimelineListAdapter.Builder(this)
            .setTimeline(userTimeline)
            .build();
    setListAdapter(adapter);
     }
    }
    

    and the corresponding XML layout timeline.xml, showing attribute values without resource references for clarity:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView android:id="@id/android:empty"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:gravity="center_horizontal|center_vertical"
              android:text="No Tweets"/>
    <ListView android:id="@id/android:list"
              android:layout_width="match_parent"
              android:layout_height="0dp"
              android:layout_weight="1"
              android:divider="#e1e8ed"
              android:dividerHeight="1dp"
              android:drawSelectorOnTop="false"/
    </LinearLayout>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search