public async void MakeRequest(string requestUrl)
{
try
{
HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
using (HttpWebResponse response = await request.GetResponseAsync() as HttpWebResponse)
{
if (response.StatusCode != HttpStatusCode.OK)
throw new Exception(string.Format(
"Server error(HTTP {0}:{1}.",
response.StatusCode,
response.StatusDescription));
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(RootObject));
object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
root = (RootObject)objResponse;
if (root == null)
this.Frame.Navigate(typeof(MainPage));
}
}
catch (Exception ex)
{
ThrowException(ex);
}
}
我的上述代码有什么问题吗?
答案 0 :(得分:5)
只需更改此行即可使用JSON而不是XML格式进行查询
所以从这......
string query = "http://dev.virtualearth.net/REST/v1/Locations/" +
queryString + "?output=xml" + " &key=" + BingMapsKey;
对此...
string query = "http://dev.virtualearth.net/REST/v1/Locations/" +
queryString + "?output=json" + " &key=" + BingMapsKey;
即。只需将output=xml
更改为output=json
答案 1 :(得分:0)
我将代码中的未定义RootObject
替换为Bing Maps REST Service .NET Libraries中的BingMapsRESTService.Common.JSON.Response
类型,而@rbrundritt建议您使用in your other post