如何从 webapi 获取数据、解析数据并将其显示在 Windows Phone 7 上
我今天早些时候发表了一篇关于从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,我建议使用 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
这是一种有效的方法吗?
Is this a valid way to do it?