Reputation: 45
i want to fetch the user home feed from twitter .
I want to know the url query by which we can fetch the user home feed . Currently authorization from twitter using OAuth lib. are done .
Is there any query url like searching tweets twitter search feed url query
by which we get home feed and user status.
What query should be written to get twitter home feed ??
Upvotes: 0
Views: 466
Reputation: 27748
Well, if you are authenticated successfully with Twitter, you don't need to query a URL to get the Timeline of the logged in user. Even if you could, I am not aware of it.
That being said, I use this to fetch the Timeline:
ConfigurationBuilder builder = new ConfigurationBuilder();
// GET THE CONSUMER KEY AND SECRET KEY FROM THE STRINGS XML
String TWITTER_CONSUMER_KEY = getString(R.string.TWITTER_CONSUMER_KEY);
String TWITTER_CONSUMER_SECRET = getString(R.string.TWITTER_CONSUMER_SECRET);
// TWITTER ACCESS TOKEN
String twit_access_token = twitPrefs.getString(PREF_KEY_OAUTH_TOKEN, "");
// TWITTER ACCESS TOKEN SECRET
String twit_access_token_secret = twitPrefs.getString(PREF_KEY_OAUTH_SECRET, "");
builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
builder.setOAuthAccessToken(twit_access_token);
builder.setOAuthAccessTokenSecret(twit_access_token_secret);
AccessToken accessToken = new AccessToken(twit_access_token, twit_access_token_secret);
Twitter twitter = new TwitterFactory(builder.build()).getInstance(accessToken);
Paging paging = new Paging(1, 20);
statuses = twitter.getHomeTimeline(paging);
// NOW LOOP THROUGH THE statuses AND FETCH INDIVIDUAL DETAILS
for (int i = 0; i < statuses.size(); i++) {
String strTweetID = String.valueOf(statuses.get(i).getId());
.... REST OF WHATEVER YOU NEED TO PULL OUT OF THE
}
Some info about instances used in the example:
1: The line: statuses = twitter.getHomeTimeline(paging);
used in the example has statuses
being an instance of List<Status>
declared as a Global variable:
List<Status> statuses = new ArrayList<Status>();
2: twitPrefs
is an instance of SharePreferences
that I use to store the OAuth Token and the OAuth Secret Key. You will need to replace that with your own method of retrieving that data.
3: The String TWITTER_CONSUMER_KEY = getString(R.string.TWITTER_CONSUMER_KEY);
and the String TWITTER_CONSUMER_SECRET = getString(R.string.TWITTER_CONSUMER_SECRET);
are pulled from the strings.xml
. Similar to the 2nd point above, use whatever method you currently employ to retrieve the said details.
ALTERNATIVE SOLUTION:
If you prefer to get a JSON response, make this change to the above code:
builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
builder.setOAuthAccessToken(twit_access_token);
builder.setOAuthAccessTokenSecret(twit_access_token_secret);
builder.setJSONStoreEnabled(true);
try {
Paging paging = new Paging(offSet, 5);
statuses = twitter.getHomeTimeline(paging);
try {
String strInitialDataSet = DataObjectFactory.getRawJSON(statuses);
JSONArray JATweets = new JSONArray(strInitialDataSet);
for (int i = 0; i < JATweets.length(); i++) {
JSONObject JOTweets = JATweets.getJSONObject(i);
String tweetID = JOTweets.getString("id");
.... OTHER DETAILS YOU NEED.
}
} catch (Exception e) {
}
} catch (Exception e) {
}
Upvotes: 1