2012-03-15 68 views
0

我想通過我的android應用程序發送Twitter推文。我使用的庫是路標核心,路標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(); 
     } 
    } 
} 

我知道回調網址是正確的。難道是我使用路標進行身份驗證並嘗試使用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);