如何从 webapi 获取数据、解析数据并将其显示在 Windows Phone 7 上

发布于 2024-10-10 02:51:15 字数 1115 浏览 6 评论 0原文

我今天早些时候发表了一篇关于从 Windows Phone 7 上的 webapi 获取数据的文章,但我认为我把事情过于复杂化了,并且不太清楚我想做什么。

现在我想做的事情是有一个方法,从 webapi 中获取 xml 中的一些数据,然后将其解析为一个类。

例如:

    public List<Alliance> getAllianceList()
    {
        const string serviceUrl = "/eve/AllianceList.xml.aspx";

        string xml = getXML(serviceUrl);

        //Some parsing logic and then returns it.
    }

在 getXML 中,我尝试使用 WebClient 或 HttpWebRequest 获取数据(不知道哪一个是最好的),然后返回它。但我的场景中的问题是它是异步的,而且我对异步没有太多了解。

到目前为止,这就是我的做法:

    private string _xml = "";
    public string getXML(string serviceUrl)
    {
        var webClient = new WebClient();
        webClient.DownloadStringCompleted += webClient_DownloadStringCompleted;
        webClient.DownloadStringAsync(new Uri(ApiUrl + serviceUrl));
    }

    void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            _xml = e.Result;
        }
    }

现在我不明白的是,如果请求是异步的,如何继续在 getAllianceList 中进行解析。 我应该使包含 getAllianceList 在内的整个“链”异步吗?如果是的话怎么办?

I made a post erlier this day about getting data from an webapi on the windows phone 7 but I think I overcomplicated things and were too unclear about what I wanted to do.

Now the thing I am trying to do is having a method going out and fetch some data in xml from a webapi and the returns it parsed to a class.

For example:

    public List<Alliance> getAllianceList()
    {
        const string serviceUrl = "/eve/AllianceList.xml.aspx";

        string xml = getXML(serviceUrl);

        //Some parsing logic and then returns it.
    }

In the getXML I am trying to get the data using the WebClient or the HttpWebRequest(Does not know wich one is the best) and then returns it. But the problem in my scenario is that it is async and I don't have much knowledge about async.

This is how I have made it so far:

    private string _xml = "";
    public string getXML(string serviceUrl)
    {
        var webClient = new WebClient();
        webClient.DownloadStringCompleted += webClient_DownloadStringCompleted;
        webClient.DownloadStringAsync(new Uri(ApiUrl + serviceUrl));
    }

    void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            _xml = e.Result;
        }
    }

Now the thing I don't understand is how to go on with the parsing in getAllianceList if the request is async.
Should I make the whole "chain" from and including getAllianceList async? And if so how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

软甜啾 2024-10-17 02:51:15

首先,我建议使用 HttpWebRequest 来避免阻塞 UI 线程。在我的帖子中进一步解释。

WebClient、HttpWebRequest 和 UI 线程在 Windows Phone 7 上

然后,您可以将回调中获得的流传递给 XDocument.Load() 来解析 XML 并用它来处理您的业务。

我下面的文章中有一个解析此 XML 和数据绑定的基本示例,该示例以 XDocument.Load() 开头(在本例中使用 XAP 文件,但原理是相同的)。

将 Linq 数据源绑定到列表框

Firstly I'd recommend HttpWebRequest to avoid blocking the UI Thread. Explained further in my post here.

WebClient, HttpWebRequest and the UI Thread on Windows Phone 7

You can then pass the stream you get in the callback to XDocument.Load() to parse the XML and do your business with it.

There's a basic sample of parsing this XML and databinding in my post below that begins with XDocument.Load() (in this case using a XAP file, but the principle is the same).

binding a Linq datasource to a listbox

情绪操控生活 2024-10-17 02:51:15

这是一种有效的方法吗?

    private const string apiUrl = "http://api.eveonline.com";
    public void UpdateAllianceList()
    {
        const string serviceUrl = "/eve/AllianceList.xml.aspx";

        var wc = new WebClient();
        wc.DownloadStringCompleted += (s, args) =>
                                          {
                                              var worker = new Thread(ParseXmlThread);;
                                              worker.Start(args.Result);
                                          };

        wc.DownloadStringAsync(new Uri(apiUrl + serviceUrl));
    }

    private void ParseXmlThread(object xml)
    {       
        // PARSING & ADDING TO THE VIEWMODEL.
    }

Is this a valid way to do it?

    private const string apiUrl = "http://api.eveonline.com";
    public void UpdateAllianceList()
    {
        const string serviceUrl = "/eve/AllianceList.xml.aspx";

        var wc = new WebClient();
        wc.DownloadStringCompleted += (s, args) =>
                                          {
                                              var worker = new Thread(ParseXmlThread);;
                                              worker.Start(args.Result);
                                          };

        wc.DownloadStringAsync(new Uri(apiUrl + serviceUrl));
    }

    private void ParseXmlThread(object xml)
    {       
        // PARSING & ADDING TO THE VIEWMODEL.
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文