如何从Xamarin.forms pcl中的Url中检索json字符串 [英] How to retrive json string from Url in Xamarin.forms pcl
问题描述
例如,我有URL http://www.pizzaboy.de/app/pizzaboy.json 由于Xamarin.forms PCL不支持WebClient,因此我使用了Xamarin文档,该文档在表单中使用Web服务. 并遵循此示例.
For example i have URL http://www.pizzaboy.de/app/pizzaboy.json As WebClient is not supported in Xamarin.forms PCL , i went with Xamarin documentation of using web services in forms and followed this example .
我已经尽我所能去获取json string了.但它不起作用.
I have tried all what i can do go get the json string . but its not working .
以下代码不适用于所提及的网址
below code doesn't work for mentioned URL
public async Task<List<TodoItem>> RefreshDataAsync ()
{
var uri = new Uri ("http://www.pizzaboy.de/app/pizzaboy.json");
HttpClient myClient = new HttpClient();
var response = await myClient.GetAsync (uri);
if (response.IsSuccessStatusCode) {
var content = await response.Content.ReadAsStringAsync ();
Items = JsonConvert.DeserializeObject <List<TodoItem>> (content);
}
}
响应自带的内容设置为null.
response comes with content set to null .
[是代理问题,上面的代码很不错]
[IT WAS PROXY ISSUE ,ABOVE CODE IS JUST FINE]
推荐答案
您的代码段对我来说都还不错,我只是尝试了一下,它对我来说就像是魅力.我在内容变量中得到了JSON字符串,我也可以反序列化它.这里正在分享我使用的代码段.
Your snippet looked all OK to me and I just gave it a try and it worked like charm for me. I got JSON string in content variable and I could deserialize it also. Here am sharing the snippet I used.
public static async Task RefreshDataAsync ()
{
var uri = new Uri ("http://www.pizzaboy.de/app/pizzaboy.json");
HttpClient myClient = new HttpClient();
var response = await myClient.GetAsync (uri);
if (response.IsSuccessStatusCode) {
var content = await response.Content.ReadAsStringAsync ();
var Items = JsonConvert.DeserializeObject <List<RootObject>> (content);
Console.WriteLine ("");
}
}
这是我对所使用的RootObject类的定义
Here is my definition for the class RootObject I used
public class RootObject
{
public string Name { get; set; }
public string Address1 { get; set; }
public int Zip { get; set; }
public string City { get; set; }
public string Phone { get; set; }
public double Lat { get; set; }
public double Lon { get; set; }
public string Link { get; set; }
}
这篇关于如何从Xamarin.forms pcl中的Url中检索json字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!