2014-02-17 16 views
1

我們使用twitter4j userTimeLine從特定用戶獲取推文。我如何使用maxId,以及如何從推文的最後一次提取中獲取推文?使用MaxId獲取來自最後一次抓取的特定用戶的推文

我的源代碼如下,

public List<Status> userTimeLine(String keyWord, int page, int count) { 

     log.info("Showing user timeline."); 

     List<Status> statuses = new ArrayList<Status>(0); 
     Paging paging = new Paging(page, count); 
     try { 
      statuses = twitter.getUserTimeline(keyWord, paging); 
     } catch (TwitterException e) { 
      log.error("Unable to find user timeline", e); 
     } 

     return statuses; 
    } 

此代碼返回100個鳴叫的第一次提取。在第二次提取中,如果用戶發佈了新的推文,它將檢索102 [100(最後提取的推文)+2(新推文)]。否則,它會爲每次抓取返回相同的100條推文。

如何解決從tweets最後一次獲取tweet的問題?

+0

請把你的代碼,我們可以看到sthg。 –

回答

2

您可以使用Paging指定tweets(通過狀態id),以獲取使用sinceId和maxId方法發佈的推文。

since_id: returns elements which id are bigger than the specified id 
max_id: returns elements which id are smaller than the specified id 

例如:

Paging paging = new Paging(1, 10).sinceId(258347905419730944L).maxId(258348815243960320L); 
List<Status> statuses = twitter.getHomeTimeline(paging); 

你可以找到很多的東西here:,也可以用STHG那樣;

Query query = new Query("from:somebody").since("2011-01-02"); 
Twitter twitter = new TwitterFactory().getInstance(); 
QueryResult result = twitter.search(query); 
+0

Semih ..你能解釋我如何得到sinceId和maxId的價值嗎? – SST

+1

而不是使用sinceId或maxId,它更有用且易於使用分頁。你必須看分頁,我會給你一個小樣本代碼:) –

+0

謝謝你的幫助Semih ..你可以發送示例代碼分頁? – SST

相關問題