我使用Twitter4j api将Twitter构建到我的Android应用程序中以查看一个用户的推文。然而,它只提出了7条推文。我想要至少20 ...这很奇怪,因为我把数量设定为50 ..任何人都可以帮忙吗?感谢
public class Twitter extends Activity{
LinearLayout tweets;
Context cont;
List<Status> statuses;
String separatorText = "test";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.twitter);
cont = this;
statuses = new ArrayList<Status>();
tweets = (LinearLayout) findViewById(R.id.twitter_main_content);
if(Network.CheckInternet(this)){
new tweets().execute();
} else {
Toast toast = Toast.makeText(this, getResources().getString(R.string.nointernet), Toast.LENGTH_SHORT);
toast.show();
}
}
public void QueryTweets(){
ScrollView sView = new ScrollView(cont);
sView.setVerticalScrollBarEnabled(false);
sView.setHorizontalScrollBarEnabled(false);
LinearLayout layout = new LinearLayout(cont);
layout.setOrientation(1);
if(statuses.size()>0){
for(int i=0; i<statuses.size(); i++){
boolean addSeparator = false;
if(i==0){
addSeparator = true;
}
View TweetItem = LayoutInflater.from(getBaseContext()).inflate(R.layout.xmllisting_tweet, null);
TweetItem.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
TextView tweet = (TextView) TweetItem.findViewById(R.id.xmllisting_tweet_title);
tweet.setText(statuses.get(i).getText());
TextView tweet_posted = (TextView) TweetItem.findViewById(R.id.xmllisting_tweet_post_details);
tweet_posted.setText(statuses.get(i).getCreatedAt().toLocaleString());
final int IntToUse = i;
TweetItem.setOnClickListener(new OnClickListener(){
public void onClick(View v){
String url = "https://twitter.com/" + statuses.get(IntToUse).getUser().getScreenName() + "/status/" + statuses.get(IntToUse).getId();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
});
//load image asynchronously - background task
ImageView imgThmb = (ImageView) TweetItem.findViewById(R.id.xmllisting_image);
loadImage downloader = new loadImage(imgThmb, false);
downloader.execute(statuses.get(i).getUser().getBiggerProfileImageURL());
//compare dates of listings to check if separator needed
String dateCompare = statuses.get(i).getCreatedAt().toGMTString().substring(0, 11);
if(!dateCompare.equals(separatorText)){
separatorText = dateCompare;
addSeparator = true;
}
if(addSeparator){
View feedSep = LayoutInflater.from(getBaseContext()).inflate(R.layout.tweet_separator, null);
feedSep.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
TextView sepText = (TextView) feedSep.findViewById(R.id.tweet_separator);
sepText.setText(separatorText);
layout.addView(feedSep);
}
layout.addView(TweetItem);
}
}
sView.addView(layout);
tweets.addView(sView);
}
private class tweets extends AsyncTask<String, Void, Long>{
protected void onPostExecute(Long result) {
QueryTweets();
}
@Override
protected Long doInBackground(String... args) {
long totalSize = 0;
try {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("xxx")
.setOAuthConsumerSecret("xxx")
.setOAuthAccessToken("xxx")
.setOAuthAccessTokenSecret("xxx");
TwitterFactory tf = new TwitterFactory(cb.build());
//AccessToken accessToken = new AccessToken(SessionVariables.twitter_access_key, SessionVariables.twitter_access_secret);
//twitter4j.Twitter twitter = tf.getInstance(accessToken);
twitter4j.Twitter twitter = tf.getInstance();
String searchText = "CFABUK";
Query qry = new Query(searchText);
qry.setCount(50);
QueryResult qr;
try{
qr = twitter.search(qry);
for(int i=0; i<qr.getTweets().size(); i++){
twitter4j.Status status = qr.getTweets().get(i);
if(searchText.equals(status.getUser().getScreenName())){
statuses.add(status);
}
}
} catch(TwitterException e){
e.printStackTrace();
}
} catch (Exception e) {
Log.e("TwitterFeed", "Error: " + e.toString());
}
return totalSize;
}
}
}
答案 0 :(得分:0)
你应该使用Paging, 看一眼: Working with timelines, pages 要么 Twitter4j Code-Examples - Paging 要么 Twitter4j Paging javadoc
编辑在你的情况下我会用这个:
以下是我如何设法获得100条推文(我正在向SQLite DB发送推文,但在您的情况下分页会相同)
void fetch_timeline(){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
List<Status> statuses;
int count= 100;
try {
statuses = Constants.TwConst.getHomeTimeline(new Paging().count(count)); // HERE'S WHAT WILL PROBABLY HELP YOU
for(Status status: statuses){
// HERE I PARSE TWEETS AND WRITE THEM TO DATABASE
}
} catch (TwitterException e) {
e.printStackTrace();
}
}