2013-07-31 89 views
0

我用Twitter4j獲得與包括hashtag狀態:Twitter4j:搜索

ConfigurationBuilder cb = new ConfigurationBuilder(); 
      cb.setDebugEnabled(true) 
       .setOAuthConsumerKey("*******") 
       .setOAuthConsumerSecret("****") 
       .setOAuthAccessToken("*****") 
       .setOAuthAccessTokenSecret("*****"); 
      TwitterFactory tf = new TwitterFactory(cb.build()); 
      Twitter twitter = tf.getInstance(); 
      Query query = new Query("source:twitter4j yusukey"); 
      QueryResult result; 
      try { 
       result = twitter.search(query); 
       for (Status status : result.getTweets()) { 
        System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText()); 
       } 
      } catch (TwitterException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

我更換了*連鍵, 但是,應用程序無法啓動

了java.lang.RuntimeException:無法開始活動ComponentInfo {com.example.app_evnt/com.example.app_evnt.SocialNetwork}:android.os.NetworkOnMainThreadException

回答

0

所有的networkcalls在主線程中都是不允許的,你可以像這樣嘗試在另一個線程:

ConfigurationBuilder cb = new ConfigurationBuilder(); 
      cb.setDebugEnabled(true) 
       .setOAuthConsumerKey("*******") 
       .setOAuthConsumerSecret("****") 
       .setOAuthAccessToken("*****") 
       .setOAuthAccessTokenSecret("*****"); 
      TwitterFactory tf = new TwitterFactory(cb.build()); 
      final Twitter twitter = tf.getInstance(); 
      Query query = new Query("source:twitter4j yusukey"); 

      new Thread(new Runnable() { 
       @Override 
       public void run() { 
        try { 
         QueryResult result = twitter.search(query); 
         for (Status status : result.getTweets()) { 
           System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText()); 
         } 
        } catch (TwitterException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 

       } 
      }).start(); 
+0

現在的應用程序啓動,但沒有打印 –

+0

可能是沒有結果被發現?嘗試在for循環外打印一些東西 –

+0

我試過了:/,但沒有打印 –