Android: Can't post tweet using OAuth and twitter?

Possibly I'm being blind, or you forgot to include some code, but it looks like the Twitter object is never constructed. So you'd get a NullPointerException when you come to use it.

Up vote 0 down vote favorite share g+ share fb share tw.

I am trying to send tweets to twitter via my android app. The libraries I'm using are signpost core, signpost commonshttp, and jtwitter. My code in my main activity is as follows: 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(); } } } I know the callback url is right. Could it be that I authenticate using signpost and try to tweet using jtwitter? Right now, I can sign into twitter to authorize the app and get redirected back to my app, but when I type something in to try to post to twitter it gets as far as twitter.

SetStatus(status); Any help would be greatly appreciated. Android twitter oauth jtwitter signpost link|improve this question asked Mar 15 at 20:35user125527383.

Possibly I'm being blind, or you forgot to include some code, but it looks like the Twitter object is never constructed. So you'd get a NullPointerException when you come to use it. Somewhere you want code along the lines of: OAuthSignpostClient oauthClient = new OAuthSignpostClient(app_token, app_secret, user_access_token, user_secret); Twitter twitter = new Twitter(null, oauthClient).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions