2012-03-15 7 views
0

私はアンドロイドアプリを通じてツイートにツイートを送信しようとしています。私が使用しているライブラリは、看板コア、看板、commonshttp、およびjtwitterです。次のように私のメインの活動で私のコードは次のとおりです。Android:OAuthとtwitterを使ってツイートを投稿することはできません

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(); 
     } 
    } 
} 

私はコールバックURLが正しいと知っています。 signpostを使って認証し、jtwitterを使ってつぶやくことができますか?今すぐ、私はアプリを許可し、私のアプリに戻ってリダイレクトするためにTwitterにサインインすることができますが、私はtwitterに投稿しようと何かを入力しようとするとtwitter.setStatus(status);

助けていただければ幸いです。

答えて

0

おそらく私は目が見えなくなっているか、コードをいくつか入れたのを忘れていたかもしれませんが、Twitterオブジェクトが構築されていないようです。だからあなたはそれを使うようになるとNullPointerExceptionを取得します。

OAuthSignpostClient oauthClient = new OAuthSignpostClient(app_token, app_secret, user_access_token, user_secret); 
Twitter twitter = new Twitter(null, oauthClient); 

どこかは、あなたの線に沿ってコードをしたいです

関連する問題