我正在尝试从android中读取用户时间线,但没有成功。调用失败,代码为400的异常 - Http请求失败。这段代码出了什么问题?
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String key = this.getString(R.string.com_twitter_sdk_android_CONSUMER_KEY);
String secret = this.getString(R.string.com_twitter_sdk_android_CONSUMER_SECRET);
TwitterConfig config = new TwitterConfig.Builder(this)
.logger(new DefaultLogger(Log.DEBUG))
.twitterAuthConfig(new TwitterAuthConfig(
key,
secret))
.build();
Twitter.initialize(config);
final UserTimeline userTimeline = new UserTimeline.Builder()
.screenName("twitterdev")
.build();
userTimeline.next((long) 0, new Callback<TimelineResult<Tweet>>() {
@Override
public void success(Result<TimelineResult<Tweet>> result) {
for (Tweet tweet : result.data.items) {
Log.d("tweet", tweet.text);
}
}
@Override
public void failure(TwitterException exception) {
Log.d("tweet", exception.getMessage());
Twitter.getInstance().getTwitterAuthConfig().getRequestCode();
}
});
}
}
答案 0 :(得分:0)
不知道此代码有什么问题。 在twitter4j库的帮助下解决了该任务。
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(key);
builder.setOAuthConsumerSecret(secret);
builder.setOAuthAccessToken(accessToken);
builder.setOAuthAccessTokenSecret(accessTokenSecret);
Configuration configuration = builder.build();
TwitterFactory factory = new TwitterFactory(configuration);
mUnauthenticatedTwitter = factory.getInstance();
new Thread(new Runnable() {
@Override
public void run() {
try {
List<Status> statuses = mUnauthenticatedTwitter.getUserTimeline("someuser");
for (Status st:statuses){
Log.d("status",st.getText());
}
} catch (TwitterException e) {
e.printStackTrace();
}
}
}).start();