如何使用 .Net(在 Windows Phone 上)阅读公共 Twitter 源

发布于 2024-10-10 06:49:39 字数 1419 浏览 0 评论 0原文

我正在尝试读取用户的公共 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

趁微风不噪 2024-10-17 06:49:39

我终于弄清楚了这一点。我只是忘记取消注释:

      <Capability Name="ID_CAP_NETWORKING"/>

WMAppManifest.xml 中的功能。一旦我这样做了,安全异常就消失了。

I finally figured this one out. I had simply forgotten to uncomment the:

      <Capability Name="ID_CAP_NETWORKING"/>

capability in the WMAppManifest.xml. Once I did that the security exception went away.

灰色世界里的红玫瑰 2024-10-17 06:49:39

您应该查看 TweetSharp。它对我来说效果很好。
http://tweetsharp.codeplex.com

You should check out TweetSharp. It's working quite well for me.
http://tweetsharp.codeplex.com

空宴 2024-10-17 06:49:39

我在自己的应用程序上遇到了类似的事情。这是我的解决方案:

WebClient twitter=new WebClient();
twitter.OpenReadCompleted += new OpenReadCompletedEventHandler(twitter_OpenReadCompleted);
twitter.OpenReadAsync(new Uri("http://api.twitter.com..."));

void twitter_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) {
   ...
}

I hit something similar on my own app. This was my solution:

WebClient twitter=new WebClient();
twitter.OpenReadCompleted += new OpenReadCompletedEventHandler(twitter_OpenReadCompleted);
twitter.OpenReadAsync(new Uri("http://api.twitter.com..."));

and

void twitter_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) {
   ...
}
◇流星雨 2024-10-17 06:49:39

我尝试了您发布的代码,没有引发异常,得到了预期的结果,一切似乎都很好。所以你的电脑、网络连接(代理等)上一定有什么东西导致了这个问题。什么是详细异常、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?

寂寞陪衬 2024-10-17 06:49:39

当以任何方式交叉引用线程时,通常会发生此异常。在这种情况下,可能与 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

君勿笑 2024-10-17 06:49:39

您可能想查看此处提供的应用程序。此应用程序向您显示指定用户名的推文,还允许您将推文导出为 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文