0

我正在尝试通过我的 android 应用程序向 twitter 发送推文。我使用的库是signpost core、signpost 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);

任何帮助将不胜感激。

4

1 回答 1

0

可能我是盲人,或者您忘记包含一些代码,但看起来 Twitter 对象从未构造过。所以当你开始使用它时你会得到一个 NullPointerException 。

在某处你想要代码如下:

OAuthSignpostClient oauthClient = new OAuthSignpostClient(app_token, app_secret, user_access_token, user_secret);
Twitter twitter = new Twitter(null, oauthClient);
于 2012-04-06T09:13:43.800 回答