2013-10-10 69 views
2

我想將特定主題的推文存儲到文件中。用於搜索的Java代碼推文

這是我的代碼。

package twitter4j.examples.search; 
import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.util.List; 
import javax.net.ssl.SSLEngineResult.Status; 
public class SearchTweets 
{ 
    public static void main(String args[]) throws TwitterException 
    { 
     Twitter twitter = TwitterFactory.getSingleton(); 
     twitter.setOAuthConsumer("[a9Ru5GLWVbI1iMfkeAHQ9A]", "[Xs0iSIbGIEZ9XS0y6zACwdjJMLPSOA6p5iAjQQmRAfs]"); 
     RequestToken requestToken = twitter.getOAuthRequestToken(); 
     AccessToken accessToken = "1878852745-4b8qrNloveM43dMnjK2KBMFUIBJj5bwVRig2Wki"; 
     BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
     while (null == accessToken) 
     { 
      System.out.println("Open the following URL and grant access to your account:"); 
      System.out.println(requestToken.getAuthorizationURL()); 
      System.out.print("Enter the PIN(if aviailable) or just hit enter.[PIN]:"); 
      String pin = br.readLine(); 
      try 
      { 
       if(pin.length() > 0) 
       { 
        accessToken = twitter.getOAuthAccessToken(requestToken, pin); 
       } 
       else 
       { 
        accessToken = twitter.getOAuthAccessToken(); 
       } 
      } 
      catch (TwitterException te) 
      { 
       if(401 == te.getStatusCode()) 
       { 
        System.out.println("Unable to get the access token."); 
       } 
       else 
       { 
        te.printStackTrace(); 
       } 
      } 
      Query query = new Query("source:twitter4j yusukey"); 
      QueryResult result = twitter.search(query); 
      for (Status status : result.getTweets()) 
      { 
       System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText()); 
      } 
      //persist to the accessToken for future reference. 
      String token = "1878852745-4b8qrNloveM43dMnjK2KBMFUIBJj5bwVRig2Wki"; 
      String tokenSecret = "1878852745-4b8qrNloveM43dMnjK2KBMFUIBJj5bwVRig2Wki"; 
      Status status = twitter.updateStatus(args[0]); 
      System.out.println("Successfully updated the status to [" + status.getText() + "]."); 
      System.exit(0); 
     } 
    } 
} 

錯誤: -

Multiple markers at this line 

    - TwitterFactory cannot be resolved 

    - Twitter cannot be resolved to a type 

請幫我解決了這個錯誤,因爲我在做這種類型的代碼第一次。

同時建議我要這個話題(java代碼存儲的tweets到文件)

+0

有無你把twitter4j jar文件添加到你的類路徑中了嗎? –

+0

是的,我添加了twitter4j jar文件 – user2865749

+0

它是否在你的類路徑..?仔細檢查一下。 – user2339071

回答