我在Android应用程序中使用twitter4j。我正在使用带有我的使用者密钥和秘密的代码,当我登录到Twitter时,第一次尝试成功登录。但是当我再次运行代码然后尝试运行时,因为我之前已通过身份验证,它使我登录但出现“获取访问令牌出错”。我不明白该如何处理..任何人都可以帮忙..我正在使用此代码登录
public class TwitterLoginActivity extends Activity {
TextView tv;
Button loginBtn;
private TwitterApp mTwitter;
private ConnectionDetector cd;
ProgressDialog pDialog;
private static SharedPreferences mSharedPreferences;
AlertDialogManager alert = new AlertDialogManager();
RequestToken requestToken;
private static final String CONSUMER_KEY = "*****************";
private static final String CONSUMER_SECRET = "*****************************************";
static String PREFERENCE_NAME = "twitter_oauth";
static final String PREF_KEY_OAUTH_TOKEN = "oauth_token";
static final String PREF_KEY_OAUTH_SECRET = "oauth_token_secret";
static final String PREF_KEY_TWITTER_LOGIN = "isTwitterLogedIn";
static final String URL_TWITTER_AUTH = "auth_url";
static final String URL_TWITTER_OAUTH_VERIFIER = "oauth_verifier";
static final String URL_TWITTER_OAUTH_TOKEN = "oauth_token";
public void storeAccessToken(AccessToken aToken) {
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putString(PREF_KEY_OAUTH_TOKEN, aToken.getToken());
editor.putString(PREF_KEY_OAUTH_SECRET, aToken.getTokenSecret());
editor.commit();}
private enum FROM {
TWITTER_POST, TWITTER_LOGIN
};
private enum MESSAGE {
SUCCESS, DUPLICATE, FAILED, CANCELLED
};
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
setContentView(R.layout.twitter_login);
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
mTwitter = new TwitterApp(this, CONSUMER_KEY, CONSUMER_SECRET);
requestToken = mTwitter.getOAuthRequestToken;
tv = (TextView) findViewById(R.id.welcome_txt);
loginBtn = (Button) findViewById(R.id.login_btn);
loginBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mTwitter.setListener(mTwLoginDialogListener);
mTwitter.resetAccessToken();
if (mTwitter.hasAccessToken() == true) {
postAsToast(null, null);
}
else {
mTwitter.authorize();
}
}
});
}
private void postAsToast(FROM twitterPost, MESSAGE success) {
switch (twitterPost) {
case TWITTER_LOGIN:
switch (success) {
case SUCCESS:
Toast.makeText(this, "Login Successful", Toast.LENGTH_LONG).show();
break;
case FAILED:
Toast.makeText(this, "Login Failed", Toast.LENGTH_LONG).show();
default:
break;
}
break;
}
}
private TwDialogListener mTwLoginDialogListener = new TwDialogListener() {
public void onError(String value) {
postAsToast(FROM.TWITTER_LOGIN, MESSAGE.FAILED);
Log.e("TWITTER", value);
mTwitter.resetAccessToken();
}
public void onComplete(String value) {
Intent intent = new Intent(TwitterLoginActivity.this,
TweetsListActivity.class);
startActivity(intent);
finish();
}
};
}
i have also used AlertDialogManager.java,ConnectionDetector.java classes in right manner. please help
最佳答案
请参阅此链接:
http://www.androidhive.info/2012/09/android-twitter-oauth-connect-tutorial
如有任何疑问,请通知我。
它工作正常...只需使用最新的jar文件即可!!
编辑:
只需在oncreate off活动中添加以下代码:
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}