如何在我的Android应用程序中使用twitter api来实现跟随按钮

时间:2013-09-03 10:52:31

标签: android twitter twitter-follow

嗨,大家好我通过Twitter实施了各种链接。

我已成功发送推文并从Twitter获取我的粉丝。现在接下来的任务是在我的应用程序中关注twitter的功能。谁能告诉我实现它的简单方法。

我被困在最后一天。无法摆脱这一点。请不要把这个问题用于投票和各种事情。如果有人可以提供任何示例代码网址并直接回答它,那就太好了。

这是我经历过的链接:

https://dev.twitter.com/docs/follow-button

Android, Twitter, 'FOLLOW US'

https://dev.twitter.com/discussions/9515

https://code.google.com/p/android-hackathon-in-fukuoka/source/browse/trunk/sodefuri/src/jp/jagfukuoka/sodefuri/TimeLineActivity.java?spec=svn167&r=167

请帮我摆脱这个问题。

哈里

2 个答案:

答案 0 :(得分:3)

我找到解决方案。我成功地能够关注任何用户。以下是工作绝对正常的链接。 http://code.google.com/p/android-hackathon-in-fukuoka/source/browse/trunk/sodefuri/src/jp/jagfukuoka/sodefuri/TimeLineActivity.java?spec=svn167&r=167

代码: -

    new TwitterTestAsync().execute(); // CALL THIS CLASS IN YOUR MAIN (CREATE) METHOD.
    private class TwitterTestAsync extends AsyncTask<Void, Void, Void>{

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub

        try{
            doTwitterTask();
        }catch(Exception e){
            e.printStackTrace();
        }

        return null;
    }

}
    private void doTwitterTask(){
    screenName  =   "chetan_bhagat";
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true)
    .setOAuthConsumerKey("WRITE HERE YOUR CONSUMER KEY")
    .setOAuthConsumerSecret("WRITE HERE YOUR CONSUMER SECRET KEY")
    .setOAuthAccessToken("WRITE YOUR TOKEN STRING")
    .setOAuthAccessTokenSecret("WRITE YOUR TOKEN SECRET STRING");
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();                 

    try {


        twitter.createFriendship(screenName);

    } catch (TwitterException e) {

        e.printStackTrace();
    }

}

答案 1 :(得分:2)

在自定义关注按钮点击事件中使用以下代码 -

     TwitterFollow apiClient = new TwitterFollow(session);
            apiClient.getFollowService().create("Screen_Name_of_person_to_follow", null, true, new Callback<User>() {
                @Override
                public void success(Result<User> result) {
                    Toast.makeText(MainActivity.this, "Thanks for following!", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void failure(TwitterException e) {
                    Toast.makeText(MainActivity.this, "Error following", Toast.LENGTH_SHORT).show();
                }
            });

将TwitterFollow类创建为 -

public class TwitterFollow extends TwitterApiClient {
public TwitterFollow(TwitterSession session) {
    super(session);
}
public FollowService getFollowService() {
    return getService(FollowService.class);
}

public interface FollowService {
    @POST("/1.1/friendships/create.json")
    public void create(@Query("screen_name") String screen_name, @Query("user_id") String user_id, @Query("follow") boolean follow, Callback<User> cb);
}

}