2015-05-09 74 views
0

我試圖在我的android應用中實現Twitter搜索。我正在使用Twitter4j, ,但我無法獲得任何推文。 我錯過了什麼?Android上的Twitter實施

ConfigurationBuilder cb = new ConfigurationBuilder(); 
     cb.setDebugEnabled(true) 
       .setOAuthConsumerKey(TWITTER_KEY) 
       .setOAuthConsumerSecret(TWITTER_SECRET) 
       .setOAuthAccessToken(ACCESS_TOKEN) 
       .setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET); 

TwitterFactory tf = new TwitterFactory(cb.build()); 
Twitter twitter = tf.getInstance(); 

try{      
         List<Status> statuses = twitter.getHomeTimeline(); 
         System.out.println("Showing home timeline."); 
         for (Status status : statuses) { 
          System.out.println(status.getUser().getName() + ":" + 
               status.getText()); 
         } }catch(Exception ex) 
        { 
         System.out.println("Error"); 
        } 
} 
+0

此代碼僅適用於家庭時間線嗎?這是否提供推文? –

+0

我想得到一般推文(沒有登錄),並且不,它不起作用... – MorZa

+1

前例外說什麼? – inmyth

回答

1

你的問題是how to implement Twitter search並在你的代碼,你的代碼是關於how to get Home Timeline

好的,這裏的工作代碼實現家庭時間線。

try { 
      List<Status> statuses; 
      Twitter twitter = new TwitterFactory().getInstance(); 
      twitter.setOAuthConsumer(consumerKeyStr, consumerSecretStr); 
      AccessToken accessToken = new AccessToken(accessTokenStr, 
        accessTokenSecretStr); 
      twitter.setOAuthAccessToken(accessToken); 

      statuses = twitter.getHomeTimeline(); 
         System.out.println("Showing Home timeline :"); 
      for (Status status : statuses) { 
       System.out.println(status.getUser().getScreenName() + " : " + status.getText()); 
      } 

     } catch (TwitterException te) { 
      te.printStackTrace(); 
     } 

輸出:

Showing Home timeline : 

TheNextWeb : Grooveshark returned from the dead this week, sketchier than ever http://t.co/8fdP1i1duI http://t.co/KcHBWNi4gD 
mashable : Roses are nice, but here are 6 truly life-changing gifts that American mothers deserve: http://t.co/kTJocxz49D 
verge : The official Sweden Vimeo account published a video promoting its waste management abilities http://t.co/9QyxQ5sMBx http://t.co/4p1SNZRazD 
arstechnica : Appeals court kills flight attendants’ challenge to electronics on planes http://t.co/cQ5dwxoyrS by @cfarivar 
mashable : Liberia is now free of Ebola, says the @WHO. But just one sick patient could spark a resurgence of cases http://t.co/Z2KwBw44kh 
Gizmodo : Running old Mac II software on a smartwatch is a great and terrible idea: http://t.co/CH5l1PG376 http://t.co/VlOcRxtksB 
TheNextWeb : Why you should consider taking a break from your career http://t.co/fSsBwzjlzv http://t.co/FvNMZ99Q1F 
SAI : Here's what 13 famous tech titans looked like in their yearbook photos http://t.co/OBERDT0otk http://t.co/S5ExeKrdQ7 
TechCrunch : Can You Fear Me Now? http://t.co/pnUiysbIAe by @rezendi 
SAI : What it's like to use Kitchensurfing, a startup that puts a professional chef in your kitchen http://t.co/2NGlQ3Nhji 
CNET : Serious runners, the smart socks you've been waiting for are here http://t.co/G8PUmOjvIq 
ForbesTech : For two decades, investors in wind energy have been buoyed by nearly $9B in tax subsidies and giveaways: http://t.co/yqP6tp20xJ 
mashable : Boy rock star in a hairnet. You go, Mick Jagger. Go. http://t.co/0uzi5EddXM http://t.co/hNkFlWve7z 
TheNextWeb : TypeDrummer turns your computer keyboard into a drum kit http://t.co/kil9aUSW42 http://t.co/fgAnOUjcvG 
verge : Shooting entirely with IMAX cameras will make the next Avengers movies something truly unique http://t.co/7AOCdJD4iF http://t.co/pvDah17nRn 
SAI : Uber could soon be the most valuable startup of all time http://t.co/2wGI8mcgWW 
toi_tech : Google now lets you order food in US 
http://t.co/IdER8GRn8V http://t.co/pUWNgJZZEf 
SAI : Why Facebook has been aggressively reminding you of your friends' birthdays this month http://t.co/ftgMEMQJzL by @Slate 
MicrosoftIndia : Take a guess in your replies & then head to #ByTheNumbers to check if you've got it right! http://t.co/hNXhbP40y6 http://t.co/RU6nfd3tQ4 
ForbesTech : It's no Tesla, but Chevy's Spark EV is a fraction of the price: http://t.co/VQGMRDbH2W http://t.co/xGJ945GcEk 

注意

沒有認證,因爲它希望你先驗證您的身份無法落實相關到Twitter什麼。簡單地說,沒有登錄你不能完成。

+0

好的,這是我的錯誤,我的意思是Twitter搜索...我只是添加了「查詢查詢=新查詢(」twitter「);」並更改了這一行「statuses = twitter.search(query).getTweets();」,它也可以作爲查詢使用。非常感謝!!! – MorZa

+0

@Mor Ahhhh ...那很好:) –