问题
Hi Guys I have gone through various links over twitter implementation.
I'm successfully able to tweet and get my followers from twitter. Now the next task is to follow us functionality of twitter in my app. Can anyone tell me the simple way to implement it.
I'm stuck from last one day. Not able to get rid of this. Please dont take this question for voting down and various things. It would be great if someone can provide me any sample code url and straight way answer to it.
Here's the links I have gone through:
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
Please help me to get rid of this problem.
harry
回答1:
I find out solution. I am successfully able to follow any user. Following is link that is working absolutely fine. http://code.google.com/p/android-hackathon-in-fukuoka/source/browse/trunk/sodefuri/src/jp/jagfukuoka/sodefuri/TimeLineActivity.java?spec=svn167&r=167
CODE:--
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();
}
}
回答2:
Use following code on your Custom Follow Button click event -
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();
}
});
And create TwitterFollow class as -
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);
}
}
来源:https://stackoverflow.com/questions/18590415/how-to-use-twitter-api-in-my-android-application-to-implement-follow-button-only