【发布时间】:2015-09-07 08:12:30
【问题描述】:
我的应用上有这个方法:
@SuppressWarnings("deprecation")
void displayTimeLine(String token, String secret) {
if (null != token && null != secret) {
List<Status> statuses = null;
try {
twitter.setOAuthAccessToken(token, secret);
statuses = twitter.getUserTimeline();
Toast.makeText(this, statuses.get(0).getText(), Toast.LENGTH_LONG)
.show();
} catch (Exception ex) {
Toast.makeText(this, "Error:" + ex.getMessage(),
Toast.LENGTH_LONG).show();
Log.d("Main.displayTimeline",""+ex.getMessage());
}
} else {
Toast.makeText(this, "Not Verified", Toast.LENGTH_LONG).show();
}
}
它会抛出这个错误:
Error:(106, 12) error: method setOAuthAccessToken in interface OAuthSupport cannot be applied to given types;
required: AccessToken
found: String,String
reason: actual and formal argument lists differ in length
我正在导入一个基于 Eclipse 的项目(旧的 twitter4j 示例),我不得不更改这些导入:
import twitter4j.http.AccessToken;
import twitter4j.http.RequestToken;
收件人:
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
这是完整的课程:
package com.aman.samples.t4jsignin;
import java.util.List;
import com.aman.t4j.activities.R;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.Toast;
public class Main extends Activity {
/** Called when the activity is first created. */
Twitter twitter;
RequestToken requestToken;
//Please put the values of consumerKy and consumerSecret of your app
public final static String consumerKey = "removed"; // "your key here";
public final static String consumerSecret = "removed"; // "your secret key here";
private final String CALLBACKURL = "T4J_OAuth://callback_main"; //Callback URL that tells the WebView to load this activity when it finishes with twitter.com. (see manifest)
/*
* Calls the OAuth login method as soon as its started
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
OAuthLogin();
}
/*
* - Creates object of Twitter and sets consumerKey and consumerSecret
* - Prepares the URL accordingly and opens the WebView for the user to provide sign-in details
* - When user finishes signing-in, WebView opens your activity back
*/
void OAuthLogin() {
try {
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(consumerKey, consumerSecret);
requestToken = twitter.getOAuthRequestToken(CALLBACKURL);
String authUrl = requestToken.getAuthenticationURL();
this.startActivity(new Intent(Intent.ACTION_VIEW, Uri
.parse(authUrl)));
} catch (TwitterException ex) {
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
Log.e("in Main.OAuthLogin", ex.getMessage());
}
}
/*
* - Called when WebView calls your activity back.(This happens when the user has finished signing in)
* - Extracts the verifier from the URI received
* - Extracts the token and secret from the URL
*/
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Uri uri = intent.getData();
try {
String verifier = uri.getQueryParameter("oauth_verifier");
AccessToken accessToken = twitter.getOAuthAccessToken(requestToken,
verifier);
String token = accessToken.getToken(), secret = accessToken
.getTokenSecret();
displayTimeLine(token, secret); //after everything, display the first tweet
} catch (TwitterException ex) {
Log.e("Main.onNewIntent", "" + ex.getMessage());
}
}
/*
* Displays the timeline's first tweet in a Toast
*/
@SuppressWarnings("deprecation")
void displayTimeLine(String token, String secret) {
if (null != token && null != secret) {
List<Status> statuses = null;
try {
twitter.setOAuthAccessToken(token, secret);
statuses = twitter.getUserTimeline();
Toast.makeText(this, statuses.get(0).getText(), Toast.LENGTH_LONG)
.show();
} catch (Exception ex) {
Toast.makeText(this, "Error:" + ex.getMessage(),
Toast.LENGTH_LONG).show();
Log.d("Main.displayTimeline",""+ex.getMessage());
}
} else {
Toast.makeText(this, "Not Verified", Toast.LENGTH_LONG).show();
}
}
}
【问题讨论】:
标签: android eclipse twitter android-studio twitter4j