status.getText()没有提供正确的HTML

时间:2014-06-06 15:14:41

标签: twitter twitter4j

我正在使用java和twitter4j。​​

问题:我没有从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 个答案:

答案 0 :(得分:1)

做了很多R&amp; 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;
 }