8

I am working on twitter, I have access token and I am also getting the tweets now I want to reply on that tweets like we did in Twitter website for this I am using the below code but it's not reply to the tweet but it updates my status please help me to solve this issue Thanks..

public void tweetReply() {
    try {
        // System.out.println("Post Id= " + itemsObj.postId);
        AccessToken a = new AccessToken(twittertokens[0], twittertokens[1]);
        Twitter twitter = new TwitterFactory().getInstance();

        twitter = new TwitterFactory().getInstance();
        twitter.setOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
        twitter.setOAuthAccessToken(a);
        Long id = new Long("236178766597087234");
        twitter.updateStatus(new StatusUpdate("@lalitrajput024  this is to test For reply option ").inReplyToStatusId(id));

    } catch (Exception e) {
        // TODO: handle exception
    }
}
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
user1445057
  • 101
  • 1
  • 5
  • i have solve this issue on stack overflow .. please follow this link http://stackoverflow.com/questions/13134629/in-android-how-directly-post-tweet-to-following-users-of-a-authenticate-user-in This Link answer check out! i hope this link can help u ! – Akash Singh Nov 05 '12 at 10:53

3 Answers3

8

I also had this problem and then i tried having the handler in the text itself.Now it seems to be working.

public static void reply(long inReplyToStatusId,String text,double latitude,double longitude,TwitterFactory factory) throws TwitterException
{   AccessToken accessToken = loadAccessToken();
    Twitter twitter = factory.getInstance();
    twitter.setOAuthConsumer(consumerKey, consumerSecrate);
    twitter.setOAuthAccessToken(accessToken);


    StatusUpdate stat= new StatusUpdate(text);

    stat.setInReplyToStatusId(inReplyToStatusId);


    GeoLocation location= new GeoLocation(latitude, longitude);
    stat.setLocation(location);

    twitter.updateStatus(stat);
 }'

and i called it like

            reply(238912922250792960l, "@praveena_kd testing reply", 12.787, 77.7578, factory);
praveena_kd
  • 525
  • 5
  • 15
0

I have not enough reputation to comment so I put this as an answer because I stuck on this for some time. This code as stated by @praveena_kd works.

String answer = "replying to @" + userNick + ": ...";
StatusUpdate statusReply = new StatusUpdate(answer);
  statusReply.setInReplyToStatusId(statusId);
  try {
    twitter.updateStatus(statusReply);
      } catch (TwitterException ex) {
      // handle exception
  }

But you have to take care to put the @UserScreenName on the reply, else it won't appear to the sender timeline! Where

String userNick = status.getUser().getScreenName();
Sandor Mezil
  • 390
  • 1
  • 4
  • 16
0

You need to add the username to the response, in Twitter4j this is known as the screenName

For example:

String username = status.getUser().getScreenName();

String answer = "@".concat(username).concat(" Here goes your response ");