我正试图通过我的Android应用程序向Twitter发送推文。我正在使用的库是路标核心,路标commonshttp和jtwitter。我主要活动中的代码如下:
public class MainActivity extends Activity implements View.OnClickListener {
static final String TAG = "TweetExample";
private Twitter twitter;
SharedPreferences prefs;
private EditText textStatus;
private static final String CONSUMER_KEY = "my key";
private static final String CONSUMER_SECRET = "my secret";
private static String ACCESS_KEY = null;
private static String ACCESS_SECRET = null;
private static final String REQUEST_URL = "http://twitter.com/oauth/request_token";
private static final String ACCESS_TOKEN_URL = "http://twitter.com/oauth/access_token";
private static final String AUTH_URL = "http://twitter.com/oauth/authorize";
private static final String CALLBACK_URL = "TweetExample://twitt";
private static CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
private static CommonsHttpOAuthProvider provider = new CommonsHttpOAuthProvider
(REQUEST_URL, ACCESS_TOKEN_URL, AUTH_URL);
//Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Retrieve the shared preferences
prefs = getSharedPreferences(USER_PREFERENCES,
Context.MODE_PRIVATE);
// Find views by id
ImageView buttonUpdate = (ImageView) findViewById(R.id.ImageView_Update);
textStatus = (EditText) findViewById(R.id.textStatus);
ImageView btnLogin = (ImageView) findViewById(R.id.ImageView_Twit);
// Add listener
buttonUpdate.setOnClickListener(this);
btnLogin.setOnClickListener(this);
// Initialize preferences
prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences arg0, String arg1) {
twitter = null;
}
});
}
public void onClick(View v) {
switch(v.getId()){
case R.id.ImageView_Update:
String status = textStatus.getText().toString();
String message = "Status set to: " + status;
Log.d(TAG, message);
// Ignore empty updates
if (status.length() == 0)
return;
// Connect to twitter.com and update your status
try {
Log.d(TAG, "1");
twitter.setStatus(status);
Log.d(TAG, "2");
} catch (TwitterException e) {
Log.e(TAG, "Twitter exception: " + e);
}
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
break;
case R.id.ImageView_Twit:
try {
String authURL = provider.retrieveRequestToken(consumer, CALLBACK_URL);
Log.d(TAG, authURL);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authURL)));
} catch (OAuthMessageSignerException e) {
e.printStackTrace();
} catch (OAuthNotAuthorizedException e) {
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
e.printStackTrace();
} catch (OAuthCommunicationException e) {
e.printStackTrace();
}
break;
}
}
@Override
public void onResume() {
super.onResume();
Uri uri = this.getIntent().getData();
if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
Log.d(TAG, uri.toString());
String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
Log.d(TAG, verifier);
try {
provider.retrieveAccessToken(consumer, verifier);
ACCESS_KEY = consumer.getToken();
ACCESS_SECRET = consumer.getTokenSecret();
Log.d(TAG, ACCESS_KEY);
Log.d(TAG, ACCESS_SECRET);
} catch (OAuthMessageSignerException e) {
e.printStackTrace();
} catch (OAuthNotAuthorizedException e) {
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
e.printStackTrace();
} catch (OAuthCommunicationException e) {
e.printStackTrace();
}
}
}
我知道回调网址是对的。可能是我使用路标进行身份验证并尝试使用jtwitter进行推文?现在,我可以登录Twitter来授权应用程序并重定向回我的应用程序,但是当我输入内容尝试发布到Twitter时它会到twitter.setStatus(status);
非常感谢任何帮助。
答案 0 :(得分:0)
可能我是盲人,或者你忘了包含一些代码,但看起来Twitter对象永远不会构建。所以当你使用它时,你会得到一个NullPointerException。
在某个地方你想要的代码如下:
OAuthSignpostClient oauthClient = new OAuthSignpostClient(app_token, app_secret, user_access_token, user_secret);
Twitter twitter = new Twitter(null, oauthClient);