Can't print json message in list using xamarin cross platform

Using xamarin cross platform so that I want to print json message from url and print to listview. I am writing the code without errors, but with some errors. I will not work. For output only verfication, I print the button on the output screen. I will show, but the list was not printed. Please edit my code.

    static ListView listview;
    static Button button;
    public MainPage()
    {
        listview = new ListView() { RowHeight = 40 };
        button = new Button() { Text = "search Again" };
        var stack = new StackLayout()
        {
            VerticalOptions = LayoutOptions.FillAndExpand,
            Children = { button, listview },
        };
        this.Content = stack;
        GetData();
    }
     async static void GetData()
    {
        WeatherReport res = new WeatherReport();
        try
        {
            string contents;
            string Url = String.Format("http://23.253.66.248:8080/icc/api/venue/search/?                lat=39.540544&lon=-104.866115&appkey=123Chesse&restName=MyMacChennai&organization=MyMacChennai");
            HttpClient hc = new HttpClient();
            contents = await hc.GetStringAsync(Url);
            res = JsonConvert.DeserializeObject<WeatherReport>(contents);
            listview.ItemsSource = res.list;
        }
        catch (System.Exception sysExc)
        {
            // Do something to log this error.
            throw;
        }
    }
 public class WeatherReport
 {
    public WeatherReport()
    {
        list = new List<WeatherReport>();
    }
    [JsonProperty(PropertyName = "cod")]
    public string cod { get; set; }
    [JsonProperty(PropertyName = "message")]
    public string message { get; set; }
    [JsonProperty(PropertyName = "cnt")]
    public int cnt { get; set; }
    [JsonProperty(PropertyName = "list")]
    public List<WeatherReport> list { get; set; }

      

0


source to share


1 answer


First, you shouldn't use async void

other than event handlers, it's bad practice. Second, it GetData()

is an asynchronous method, however you call it synchronously in the constructor. I would suggest redesigning this a bit so that it is completely asynchronous. You might get into trouble as you wait for the code to complete before continuing. Everything else in the code looks fine.



Here are some additional resources that can help with async / await. http://msdn.microsoft.com/en-us/magazine/jj991977.aspx , async / await - when to return task vs void?

0


source







All Articles