我已经在我的 Android 应用程序中实现了 Twitter。我还实现了登录和注销功能。它工作正常,但是当我再次尝试登录 Twitter 时,它无法正常工作。它给出了以下例外:
D/Twitter-WebView(545): Loading URL: https://twitter.com/oauth/authorize
D/dalvikvm(545): GC_FOR_MALLOC freed 13203 objects / 516760 bytes in 74ms
D/NativeCrypto(545): Freeing OpenSSL session
D/Twitter-WebView(545): Redirecting URL twitterapp://connect?oauth_token=Lfbjv4ciEma8bv4XijpY4eeZrkkf735Oz1WX2BtZM&oauth_verifier=fgb9EJUG6qS92Qkk4ZYDYwtJSQzaTKkB4JGOLllv4
I/global(545): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
D/TwitterApp(545): Error getting access token
W/System.err(545): java.lang.IllegalStateException: consumer key/secret pair already set.
W/System.err(545): at twitter4j.TwitterOAuthSupportBaseImpl.setOAuthConsumer(TwitterOAuthSupportBaseImpl.java:234)
W/System.err(545): at twitter4j.Twitter.setOAuthConsumer(Twitter.java:54)
W/System.err(545): at com.twit.sb.more_module.TwitterApp.configureToken(TwitterApp.java:67)
W/System.err(545): at com.twit.sb.more_module.TwitterApp.access$7(TwitterApp.java:65)
W/System.err(545): at com.twit.sb.more_module.TwitterApp$3.run(TwitterApp.java:140)
这是我登录和注销 twitter 的代码:
if (mTwitter.hasAccessToken()) {
onTwitterClick();
} else {
mTwitter.resetAccessToken();
mTwitter.authorize();
}
private void onTwitterClick() {
if (mTwitter.hasAccessToken()) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Log out from Twitter ?")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
mTwitter.resetAccessToken();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
} else {
mTwitter.authorize();
}
}
public void authorize() {
mProgressDlg.setMessage("Initializing ...");
mProgressDlg.show();
new Thread() {
@Override
public void run() {
String authUrl = "";
int what = 1;
try {
authUrl = mHttpOauthprovider.retrieveRequestToken(mHttpOauthConsumer, CALLBACK_URL);
what = 0;
Log.d(TAG, "Request token url " + authUrl);
} catch (Exception e) {
Log.d(TAG, "Failed to get request token");
e.printStackTrace();
}
mHandler.sendMessage(mHandler.obtainMessage(what, 1, 0, authUrl));
}
}.start();
}