How to retrive json string from Url in Xamarin.forms pcl

限于喜欢 提交于 2021-02-05 06:34:09

问题


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 .

Link

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);
                }
}

response comes with content set to null .

[IT WAS PROXY ISSUE ,ABOVE CODE IS JUST FINE]


回答1:


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 ("");
        }
    }

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; }
}


来源:https://stackoverflow.com/questions/37000151/how-to-retrive-json-string-from-url-in-xamarin-forms-pcl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!