简体   繁体   中英

Android Twitter4J newest and most popular Tweets

I would like to get the newest and most popular tweets with the hashtag "proverbs" using Twitter4J , but when I try to do it, the results I get are not the newest and most popular (I know that because I compared them with the results I got while searching the same hashtag on twitter and they are not the same). Here is my code:

public class Wisdom extends SherlockActivity {
Context mContext;
Twitter mTwitter;
ListView mListView;

 @Override
 public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hastag_wisdom);
mListView = (ListView) findViewById(R.id.listView1);
mContext = getApplicationContext();
mTwitter = getTwitter();

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
        .permitAll().build();
StrictMode.setThreadPolicy(policy);
showTweetsAbout("#proverbs");

}

 private Twitter getTwitter() {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey(xxxxxxx);
cb.setOAuthConsumerSecret(xxxxxxx);
cb.setOAuthAccessToken(xxxxxxx);
cb.setOAuthAccessTokenSecret(xxxxxxx);
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
return twitter;
} 

private void showTweetsAbout(String queryString) {
ArrayList<String> tweetsArray = new ArrayList<String>();

Twitter twitter = mTwitter;
Query query = new Query(queryString);
query.setResultType(Query.MIXED);
query.setCount(30);
QueryResult result;

try {
    result = twitter.search(query);

    for (Status tweet : result.getTweets()) {
        Log.d("Wisdom", tweet.getUser() + ":" + tweet.getText());
        tweetsArray.add(tweet.getText());
        for (URLEntity urle : tweet.getURLEntities()) {
            System.out.println(urle.getDisplayURL());
        }
    }
} catch (TwitterException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
ArrayAdapter<String> tweetsAdapter = new ArrayAdapter<String>(this,
        R.layout.row, tweetsArray);

mListView.setAdapter(tweetsAdapter);
}
}

Code would also be greatly appreciated swell as an explanation, thanks.

Despite the fact that you're doing network operation on main thread (!!) Try to specifiy in query result untill date

query.setUntill(date);

As a date set current time, it returns tweets generated before given date.

Another question is how much results from twitter4j differ from tweets on site ? It's possible that in meantime between doing a query in mobile app and querying website a few new tweets with given hashtag came up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM