簡體   English   中英

Twitter4j-僅顯示用戶幾條推文-Android

[英]Twitter4j--only bringing up few tweets from user - Android

我已使用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;
        }       
    }
}

您應該使用Paging,看看: 使用時間軸,頁面Twitter4j代碼示例 -PagingTwitter4j Paging javadoc

編輯你的情況,我會用這個: 來自Twitter4j doc

這就是我已經設法獲得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();

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM