I want to post tweet to following users of a Authenticate user.For authenticate using Twitter-4j library .I have get list(Name & id) of following users but not able to post tweet without open dialog.I am usingthis link for authenticate
Question-How directly post tweet to following users of a authenticate user in android wihout open Tweet dialog box(Message Dialog)
1. on twitterButton click a new Activity open with webview
twitterButton=(Button) findViewById(R.id.twitter);
twitterButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mTwitter = new TwitterFactory().getInstance();
mRequestToken = null;
mTwitter.setOAuthConsumer(TwitterConstants.CONSUMER_KEY,
TwitterConstants.CONSUMER_SECRET);
String callbackURL = getResources().getString(
R.string.twitter_callback);
try {
mRequestToken = mTwitter.getOAuthRequestToken(callbackURL);
System.out.println("URL"
+ mRequestToken.getAuthenticationURL());
} catch (TwitterException e) {
e.printStackTrace();
}
Intent i = new Intent(MMSExampleActivity.this,
TwitterScreen.class);
i.putExtra("URL", mRequestToken.getAuthenticationURL());
System.out.println("Url ==== "
+ mRequestToken.getAuthenticationURL());
startActivityForResult(i, TWITTER_AUTH);
}
});
2. in onActivityResult(int requestCode, int resultCode, Intent data) method
if (resultCode == Activity.RESULT_OK) {
String oauthVerifier = (String) data.getExtras().get(
"oauth_verifier");
AccessToken at = null;
try {
// Pair up our request with the response
at = mTwitter.getOAuthAccessToken(mRequestToken,
oauthVerifier);
accessToken = at.getToken();
System.out.println("access token" + accessToken);
accessTokenSecret = at.getTokenSecret();
getFollowers();
Intent twitterFriendIntent=new Intent(MMSExampleActivity.this,TwitterFriends.class);
twitterFriendIntent.putExtra("twitterfriends", twitterFriends);
startActivity(twitterFriendIntent);
} catch (TwitterException e) {
System.out.println("e........");
e.printStackTrace();
}
}
3. Getting following userList
public void getFollowers()
{
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(TwitterConstants.CONSUMER_KEY);
builder.setOAuthConsumerSecret(TwitterConstants.CONSUMER_SECRET);
builder.setOAuthAccessToken(accessToken);
builder.setOAuthAccessTokenSecret(accessTokenSecret);
Configuration conf = builder.build();
twitter = new TwitterFactory(conf).getInstance();
try {
long lCursor = -1;
IDs friendsIDs = twitter.getFriendsIDs(twitter.getId(), lCursor);
IDs followersIds=twitter.getFollowersIDs(twitter.getId(), lCursor);
System.out.println(twitter.showUser(twitter.getId()).getName());
System.out.println("==========================");
do
{
for (long i : friendsIDs.getIDs())
{
FriendList friendListObj=new FriendList();
friendListObj.setTwitterId(i);
friendListObj.setTwitterUsername(twitter.showUser(i).getName());
friendListObj.setTwitterUrl(twitter.showUser(i).getScreenName());
twitterFriends.add(friendListObj);
System.out.println("follower ID #" + i);
System.out.println(twitter.showUser(i).getName());
System.out.println(twitter.showUser(i).getProfileImageURL());
System.out.println(twitter.showUser(i).getURL());
}
}while(friendsIDs.hasNext());
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
4. Code for post tweets
public void updateStatus( String messageToPost) {
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(TwitterConstants.CONSUMER_KEY);
builder.setOAuthConsumerSecret(TwitterConstants.CONSUMER_SECRET);
builder.setOAuthAccessToken(accessToken);
builder.setOAuthAccessTokenSecret(accessTokenSecret);
Configuration conf = builder.build();
Twitter twitter = new TwitterFactory(conf).getInstance();
System.out.println("in update status");
try {
// twitter.updateStatus("Hello World!");
StatusUpdate status = new StatusUpdate(messageToPost);
System.out.println("Length of Message is = = = "
+ messageToPost.trim().length());
System.out.println("App" + file);
status.setMedia(file);
System.out.println("App" + file.exists());
twitter.updateStatus(status);
System.out.println("App" + file);
} catch (TwitterException e) {
System.err.println("Error occurred while updating the status!");
e.printStackTrace();
}
}
U have got Twitter Friends ID ... now get Friends Detail(basically Screen Name)
// now u have got Friend ScreenName (FriendScreename_).this ScreenName use for post tweet to paticuler Friend
//Twitter class
TwitterDialog class
TwitterSession
following jar file use
where . Friend_ID is String Arraylist.. I hope this code can help u .Enjoy This code !