我試圖從Twitter獲取信息,我跟着這個鏈接:如何在android.content.Context和android.app.Activity中創建上下文?
http://code.google.com/p/sociallib/wiki/SocialLibGuide
我不明白下面兩行,它顯示錯誤下面幾行。
twitter.requestAuthorization(this);
twitter.authorize(this);
下面添加完整的代碼。
它說,anroid.content.Context和android.app.Activity是必需的。我真的不知道如何添加它們。任何幫助表示讚賞。
package sociallibjar;
import android.R;
import android.content.Context;
import com.expertiseandroid.lib.sociallib.connectors.SocialNetworkHelper;
import com.expertiseandroid.lib.sociallib.connectors.TwitterConnector;
import com.expertiseandroid.lib.sociallib.model.twitter.TwitterUser;
import org.scribe.oauth.Token;
public class TwitterApp {
String CONS_KEY = "";
String CONS_SEC = "";
String CALLBACK = "";
public void twiter() {
try {
TwitterConnector twitter = SocialNetworkHelper.createTwitterConnector(CONS_KEY, CONS_SEC, CALLBACK);
twitter.requestAuthorization(this);
twitter.authorize(this);
Token at = twitter.getAccessToken();
String token = at.getToken(); //You can store these two strings
String secret = at.getSecret();//in order to build the token back
Token myAccessToken = new Token(token, secret);
twitter.authentify(myAccessToken);
TwitterUser me = twitter.getUser(); //Retrieves the current user
String nickname = me.getUsername(); //Some information is available through method calls
int nbFollowers = me.nbFollowers; //Some is available through object fields
System.out.println("You are logged in as " + nickname + " and you have " + nbFollowers + " followers"); //Now we can print the information we retrieved onscreen
twitter.tweet("Tweeting from code"); //A simple tweet
twitter.tweet("I have " + twitter.getUser().nbFollowers + " followers!");//A more elaborate tweet
//twitter.logout(this); //Providing this code is located in an Activity (or Context) class
} catch (Exception e) {
}
}
}
請給多一點的代碼,如在哪裏被寫入這兩條線。 – MByD 2012-02-27 11:18:53