sak*_*mar 3 c# json http xamarin xamarin.forms
例如,我有一个 URL http://www.pizzaboy.de/app/pizzaboy.json 由于 Xamarin.forms PCL 不支持 WebClient,因此我查阅了在表单中使用 Web 服务的 Xamarin 文档并遵循了此示例。
我已经尝试了所有我能做的来获取 json 字符串。但这不起作用。
以下代码不适用于提到的 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);
}
}
Run Code Online (Sandbox Code Playgroud)
响应的内容设置为 null 。
[免责声明:这是网络代理问题阻止了上述网址,上面的代码就可以了]
你的片段对我来说看起来没什么问题,我只是尝试了一下,它对我来说就像魅力一样。我在内容变量中得到了 JSON 字符串,我也可以反序列化它。这里分享我使用的片段。
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 ("");
}
}
Run Code Online (Sandbox Code Playgroud)
这是我使用的类 RootObject 的定义
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; }
}
Run Code Online (Sandbox Code Playgroud)