我是 android 开发的新手(和一般的 java)。我正在构建一个应用程序,该应用程序要求用户通过登录他们的 Twitter 帐户进行注册,从该帐户导入和保存他们的基本信息。所需的信息是生日、姓名(全名)、用户名、位置、性别等。 我正在使用 twitter4j 来实现这一点。我试过查看 twitter4j 文档,但作为一个 android 新手(和一般的 java),文档有点难以理解,所以我似乎无法让它做一些除了设置状态更新之外的事情。
这是我 Activity 中的代码:
package cc.co.localsquare.app;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast;
import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.exception.OAuthNotAuthorizedException;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.http.AccessToken;
public class ConnectTwitterActivity extends BaseActivity {
public final static String CONSUMER_KEY = "valid key";
public final static String CONSUMER_SECRET = "valid secret";
public final static String CALLBACK_URL = "localsquare://ConnectTwitterActivity2";
private CommonsHttpOAuthConsumer commonHttpOAuthConsumer;
private OAuthProvider authProvider;
private Twitter twitter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.connect_twitter);
commonHttpOAuthConsumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
authProvider = new DefaultOAuthProvider("http://twitter.com/oauth/request_token",
"http://twitter.com/oauth/access_token", "http://twitter.com/oauth/authorize");
try {
String oAuthURL = authProvider.retrieveRequestToken(commonHttpOAuthConsumer, CALLBACK_URL);
this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(oAuthURL)));
} catch (OAuthMessageSignerException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (OAuthNotAuthorizedException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (OAuthCommunicationException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Uri uri = intent.getData();
if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
String verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
try {
authProvider.retrieveAccessToken(commonHttpOAuthConsumer, verifier);
AccessToken accessToken = new AccessToken(commonHttpOAuthConsumer.getToken(),
commonHttpOAuthConsumer.getTokenSecret());
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
twitter.setOAuthAccessToken(accessToken);
// Alternate way:
// twitter = new TwitterFactory().getOAuthAuthorizedInstance(CONSUMER_KEY, CONSUMER_SECRET,
// new AccessToken(commonHttpOAuthConsumer.getToken(), commonHttpOAuthConsumer.getTokenSecret()));
// Tweet message to be updated.
String tweet = "Hi there, This was sent from my application - OAuth, Twitter";
twitter.updateStatus(tweet);
}
catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG);
}
}
}
}
我该如何解决我的问题?
最佳答案
如果您有需要获取详细信息的 twitter 用户的屏幕名称,则可以使用 User user = twitter.showUser(screenName);
。现在您可以访问信息通过在对象 user
上使用 getter 函数来了解用户,例如user.getName()
、user.getLocation()
等
记得在 Eclipse 中导入 User
类。通常你也可以通过在 Eclipse 中按 Ctrl+Shift+O 来完成。
关于android - 如何使用 Twitter 4j 检索 Twitter 用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8774650/