如何使用 .Net(在 Windows Phone 上)阅读公共 Twitter 源
我正在尝试读取用户的公共 Twitter 状态,以便可以在我的 Windows Phone 应用程序中显示它。
我用 Scott Gu 的例子: http://weblogs.asp.net/scottgu/archive/2010/03/18/building-a-windows-phone-7-twitter-application-using-silverlight.aspx
当我的代码从在异步调用中,当我尝试使用 e.Result 时,我会收到“System.Security.SecurityException”。
我知道我的 uri 是正确的,因为我可以将其放入浏览器中并获得良好的结果。
这是我的相关代码:
public void LoadNewsLine()
{
WebClient twitter = new WebClient();
twitter.DownloadStringCompleted += new DownloadStringCompletedEventHandler(twitter_DownloadStringCompleted);
twitter.DownloadStringAsync(new Uri("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=krashlander"));
}
void twitter_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
XElement xmlTweets = XElement.Parse(e.Result); //exception thrown here!
var message = from tweet in xmlTweets.Descendants("status")
select tweet.Element("text").Value;
//Set message and tell UI to update.
//NewsLine = message.ToString();
//RaisePropertyChanged("NewsLine");
}
有人有什么想法吗?
解决方案: 我终于弄清楚了这一点。我只是忘记取消注释: WMAppManifest.xml 中的功能。 一旦我这样做了,安全异常就消失了。
I'm trying to read the public twitter status of a user so I can display it in my Windows Phone application.
I'm using Scott Gu's example:
http://weblogs.asp.net/scottgu/archive/2010/03/18/building-a-windows-phone-7-twitter-application-using-silverlight.aspx
When my code comes back from the async call, I get a "System.Security.SecurityException" as soon as I try to use the e.Result.
I know my uri is correct because I can plop it in the browser and get good results.
Here is my relavent code:
public void LoadNewsLine()
{
WebClient twitter = new WebClient();
twitter.DownloadStringCompleted += new DownloadStringCompletedEventHandler(twitter_DownloadStringCompleted);
twitter.DownloadStringAsync(new Uri("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=krashlander"));
}
void twitter_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
XElement xmlTweets = XElement.Parse(e.Result); //exception thrown here!
var message = from tweet in xmlTweets.Descendants("status")
select tweet.Element("text").Value;
//Set message and tell UI to update.
//NewsLine = message.ToString();
//RaisePropertyChanged("NewsLine");
}
Any ideas anyone?
SOLUTION:
I finally figured this one out. I had simply forgotten to uncomment the:
capability in the WMAppManifest.xml.
Once I did that the security exception went away.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我终于弄清楚了这一点。我只是忘记取消注释:
WMAppManifest.xml 中的功能。一旦我这样做了,安全异常就消失了。
I finally figured this one out. I had simply forgotten to uncomment the:
capability in the WMAppManifest.xml. Once I did that the security exception went away.
您应该查看 TweetSharp。它对我来说效果很好。
http://tweetsharp.codeplex.com
You should check out TweetSharp. It's working quite well for me.
http://tweetsharp.codeplex.com
我在自己的应用程序上遇到了类似的事情。这是我的解决方案:
和
I hit something similar on my own app. This was my solution:
and
我尝试了您发布的代码,没有引发异常,得到了预期的结果,一切似乎都很好。所以你的电脑、网络连接(代理等)上一定有什么东西导致了这个问题。什么是详细异常、StackTrace?也许 在 Windows Phone 7 上简单使用 WebClient 会抛出异常NetworkError 会对你有帮助吗?
I tried code you posted and no exception was thrown, getting results as expected, all seems to be just fine. So there must be something on your pc, net connection (proxy etc) which is causing this. What is detailed exception, StackTrace? Maybe Simple use of WebClient on Windows Phone 7 throws a NetworkError will help you?
当以任何方式交叉引用线程时,通常会发生此异常。在这种情况下,可能与 Webclient 始终在 UI 线程上返回有关。您也许可以通过使用 HttpWebRequest 来解决问题(无论如何,这在性能方面是更好的)。
检查 msdn 上的此主题论坛了解更多信息
This exception usually occurs when cross referencing threads in any way. In this case it is probably related to Webclient always returning on the UI thread. You can probably solve the problem by using HttpWebRequest instead (which is better performance wise anyway).
Check this thread on msdn forums for more info
您可能想查看此处提供的应用程序。此应用程序向您显示指定用户名的推文,还允许您将推文导出为 pdf、excel、word、图像等。
http://blogs.gcpowertools.co.in/2012/05/activereporting-with-twitter.html
You may want to have a look at the application provided here .This application show you tweets for a specified username and also allows you to export tweets to pdf,excel,word,image etc ..
http://blogs.gcpowertools.co.in/2012/05/activereporting-with-twitter.html