2012-03-15 30 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);