如何代表特定的hashtag / keyword获取所有推文并在View页面上显示?找到的唯一解决方案返回null异常。
public ActionResult TweetView(string txtTwitterName)
{
//TwitterService("consumer key", "consumer secret");
var service = new TwitterService("", "");
//AuthenticateWith("Access Token", "AccessTokenSecret");
service.AuthenticateWith("", "");
TwitterSearchResult tweets = service.Search(new SearchOptions { Q = txtTwitterName, SinceId=29999 });
IEnumerable<TwitterStatus> status = tweets.Statuses;
ViewBag.Tweets = tweets;
return View();
}
查看:
IEnumerable<TwitterStatus> tweets = ViewBag.Tweets as IEnumerable<TwitterStatus>;
foreach (var tweet in tweets)
{
<div class="tweet">
<div class="picture">
<img src="@tweet.User.ProfileImageUrl" alt="@tweet.User.ScreenName" title="@tweet.User.ScreenName" />
</div>
<div class="info">
<span>@tweet.User.Name, @tweet.User.Description - @tweet.User.Location </span>
<br />
<a href="https://twitter.com/statuses/@tweet.Id" class="text">
@tweet.Text
</a>
<div class="action">
@tweet.CreatedDate.AddHours(3).ToString("d/M/yyyy HH:mm:ss")
</div>
</div>
<div class="clear">
</div>
</div>
}
答案 0 :(得分:1)
我在同样的问题上挣扎。这是我模糊的解决方案。只要返回所需数量的推文,该函数就会返回。我希望它可以帮助你。
string maxid = "1000000000000"; // dummy value
int tweetcount = 0;
if (maxid != null)
{
var tweets_search = twitterService.Search(new SearchOptions { Q = keyword, Count = Convert.ToInt32(count) });
List<TwitterStatus> resultList = new List<TwitterStatus>(tweets_search.Statuses);
maxid = resultList.Last().IdStr;
foreach (var tweet in tweets_search.Statuses)
{
try
{
ResultSearch.Add(new KeyValuePair<String, String>(tweet.Id.ToString(), tweet.Text));
tweetcount++;
}
catch { }
}
while (maxid != null && tweetcount < Convert.ToInt32(count))
{
maxid = resultList.Last().IdStr;
tweets_search = twitterService.Search(new SearchOptions { Q = keyword, Count = Convert.ToInt32(count), MaxId = Convert.ToInt64(maxid) });
resultList = new List<TwitterStatus>(tweets_search.Statuses);
foreach (var tweet in tweets_search.Statuses)
{
try
{
ResultSearch.Add(new KeyValuePair<String, String>(tweet.Id.ToString(), tweet.Text));
tweetcount++;
}
catch { }
}
}
}
答案 1 :(得分:1)
表示您的视图中的空值:
IEnumerable<TwitterStatus> status = ViewBag.Tweets as IEnumerable<TwitterStatus>;
if(status != null )
{
foreach (var tweet in status)
{
<div class="tweet">
<div class="picture">
<img src="@tweet.User.ProfileImageUrl" alt="@tweet.User.ScreenName"
title="@tweet.User.ScreenName" />
</div>
<div class="info">
<span>@tweet.User.Name, @tweet.User.Description - @tweet.User.Location </span>
<br />
<a href="https://twitter.com/statuses/@tweet.Id" class="text">
@tweet.Text
</a>
<div class="action">
@tweet.CreatedDate.AddHours(3).ToString("d/M/yyyy HH:mm:ss")
</div>
</div>
<div class="clear">
</div>
</div>
}
}
并为您的Controller修改最后一个代码:
ViewBag.Tweets = status;
我希望这篇文章有所帮助,thx :)