2014-06-06 69 views
0

我正在使用java和twitter4j。status.getText()沒有給出正確的HTML

問題:我沒有得到來自status.getText正確的響應()方法,鏈接引用來作爲文本

我有一個問題。我的方法如下:

Twitter twitter = null; 

        String userName = "clientname"; 
        int numberOfTweets = 0; 
        StringBuilder timeLineText = new StringBuilder(); 

        try { 
          twitter = new TwitterFactory(new GetAuthenticConfiguration().getConfigObject()).getInstance(); 
          ResponseList<Status> statuses = twitter.getUserTimeline(userName); 
          timeLineText.append("<li>"); 

          for (Status status : statuses) { 
           numberOfTweets++; 
           if (numberOfTweets > 12) { 
            break; 
           } 
           int remainder = numberOfTweets % 3; 

           if (remainder == 0) { 
            timeLineText.append("</li><li>"); 
           } else { 
            StringBuilder tempText = new StringBuilder(); 
            try { 
             tempText.append("<p>"); 
             tempText.append("<span>"); 
             tempText.append("<img alt=\"" + status.getUser().getScreenName() + "\" src=\"" + status.getUser().getMiniProfileImageURL() + "\" />"); 
             tempText.append("<a href=\"https://twitter.com/" + status.getUser().getScreenName() + "\" target=\"_blank\"><b>" + status.getUser().getName() + "</b> @" + status.getUser().getScreenName() + "</a> " + new SimpleDateFormat("dd MMM").format(status.getCreatedAt())); 
             tempText.append("</span>"); 
             tempText.append("<p>" + status.getText() + "</p>"); 
             tempText.append("</p>"); 
            } catch (Exception e) { 
             System.out.println(e); 
            } 

            timeLineText.append(tempText); 
           } 
          } 
          timeLineText.append("</li>"); 
        } catch (Exception te) { 
          System.out.println(te); 
        } 

我得到的迴應是:中

Can you tell us which Rolls-Royce #engine powers this aircraft? #AvGeek http://t.co/t5tNXQuMFB 

代替

Can you tell us which Rolls-Royce <a href="/hashtag/engine?src=hash" data-query-source="hashtag_click" class="twitter-hashtag pretty-link js-nav" dir="ltr" ><s>#</s><b>engine</b></a> powers this aircraft? <a href="/hashtag/AvGeek?src=hash" data-query-source="hashtag_click" class="twitter-hashtag pretty-link js-nav" dir="ltr" ><s>#</s><b>AvGeek</b></a> <a href="http://t.co/t5tNXQuMFB" class="twitter-timeline-link u-isHiddenVisually" data-pre-embedded="true" dir="ltr" >pic.twitter.com/t5tNXQuMFB</a> 

的問題是,我沒有得到正確的HTML。任何人都可以告訴我它的原因嗎?

回答

1

經過大量的R & D,我發現Status.getText()方法只給出純文本。 我們必須手動將其轉換爲鏈接,主題標籤和用戶。 下面是我寫這樣做的方法。 只需將您的getText()輸出傳遞給此方法(tweet)即可。 我希望這會對Twitter4J的許多初學者有所幫助。

private String linkifyTweet(String tweet) { 
     Pattern pattern; 
     Matcher matcher; 

     String regex_url = "((https?://\\S+)|(www.\\S+))"; 
     String regex_hashtag = "#(\\w+)"; 
     String regex_user = "@(\\w+)"; 

     //regex to apply links to all urls in the tweet 
     pattern = Pattern.compile(regex_url); 
     matcher = pattern.matcher(tweet); 
     if (matcher.find()) { 
      tweet = tweet.replaceAll(regex_url, "<a target=\"_blank\" href=\"$1\">$1</a>"); 
     } 

     //regex to apply links to all hashtags in the tweet 
     pattern = Pattern.compile(regex_hashtag); 
     matcher = pattern.matcher(tweet); 
     if (matcher.find()) { 
      tweet = tweet.replaceAll(regex_hashtag, "<a target=\"_blank\" href=\"https://www.twitter.com/hashtag/$1?src=hash\">#$1</a>"); 
     } 

     //regex to apply links to all users in the tweet 
     pattern = Pattern.compile(regex_user); 
     matcher = pattern.matcher(tweet); 
     if (matcher.find()) { 
      tweet = tweet.replaceAll(regex_user, "<a target=\"_blank\" href=\"https://www.twitter.com/$1\">@$1</a>"); 
     } 
     //System.out.println(tweet); 
     return tweet; 
} 
+0

這節省了我很多時間,謝謝@Vasant –