0

I'm trying do a simple app in Xamarin where I can see the current weather in my city. But I cant seem to get it right.. I've got the api from openweathermap.org, where in the url I'm searching for my city's coordinates. To get the classes for my "OpenWeather" class, I used json2csharp, to generate my class.

I want to display the city name, weatherdescription and the coordinates.

I'll post the code I got so far:

MainPage.xaml.cs:

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        GetWeather();

    }

    public async void GetWeather()
    {

        try
        {
            string URL = "http://api.openweathermap.org/data/2.5/weather?lat=58.34&lon=11.94&appid=((((((MY API KEY))))))))))))";

            HttpClient httpClient = new HttpClient();
            HttpResponseMessage response = await httpClient.GetAsync(new Uri(URL));

            if (response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync();
                var weatherList = JsonConvert.DeserializeObject<RootObject>(content);

                //Databind the list
                lstWeather.ItemsSource = weatherList;
            }
        }
        catch (Exception ex)
        {
            //ToDo Give errormessage to user and possibly log error
            System.Diagnostics.Debug.WriteLine(ex.ToString());
        }
    }
}

MainPage.xaml:

<ProgressBar IsEnabled="True"></ProgressBar>
<ListView x:Name="lstWeather">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout>
                    <StackLayout Orientation="Vertical">-->
                        <Label  Text="{Binding name}" />
                        <Label  Text="{Binding desciption}" Margin="20,0,0,0"/>
                        <Label  Text="{Binding lon}" Margin="20,0,0,0"/>
                        <Label  Text="{Binding lat}" Margin="20,0,0,0"/>
                   </StackLayout>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>


namespace TestApp

{ public class Coord { public double lon { get; set; } public double lat { get; set; } }

public class Weather
{
    public int id { get; set; }
    public string main { get; set; }
    public string description { get; set; }
    public string icon { get; set; }
}

public class Main
{
    public double temp { get; set; }
    public int pressure { get; set; }
    public int humidity { get; set; }
    public double temp_min { get; set; }
    public double temp_max { get; set; }
}

public class Wind
{
    public double speed { get; set; }
    public int deg { get; set; }
}

public class Clouds
{
    public int all { get; set; }
}

public class Sys
{
    public int type { get; set; }
    public int id { get; set; }
    public double message { get; set; }
    public string country { get; set; }
    public int sunrise { get; set; }
    public int sunset { get; set; }
}

public class RootObject
{
    public Coord coord { get; set; }
    public List<Weather> weather { get; set; }
    public string @base { get; set; }
    public Main main { get; set; }
    public int visibility { get; set; }
    public Wind wind { get; set; }
    public Clouds clouds { get; set; }
    public int dt { get; set; }
    public Sys sys { get; set; }
    public int id { get; set; }
    public string name { get; set; }
    public int cod { get; set; }
}
2
  • 1
    "can't seem to get it right" is not a helpful description of your problem. What specifically are you having trouble with? Are you getting an error or exception? Commented Oct 6, 2017 at 11:20
  • @Jason Oh sorry! My exception for the moment is in my MainPage.xaml.cs file where the errormessage is: "Cannot Implicity convert type 'TestApp.RootObject' to 'System.Collections.IEnumerable'. An explicit conversion exists (are you missing a cast?) I'm not even sure if my syntax is right to print out what I want, when that problem is fixed.. Commented Oct 6, 2017 at 11:24

2 Answers 2

1

the problem is here:

lstWeather.ItemsSource = weatherList;

weatherList is an RootObject, but a ListView requires an IEnumerable for it's datasource

You probably want to do this:

lstWeather.ItemsSource = weatherList.weather;
Sign up to request clarification or add additional context in comments.

12 Comments

That helped me get rid of my error. Now I can start the app, but its blank. If I hover with my mouse I can see that there's a listitem there. When I put a breakpoint at lstWeather.ItemsSource = weatherList.weather; I can see the object I want to see. Maybe my databinding in the xaml is wrong?
each individual item in your list is a Weather object, so your bindings need to be properties of Weather
Well, I get this in the output window though.. imgur.com/a/n4qZF How will the databinding be then? <Label Text="{Binding weather.name}" /> ?
the weather class doesn't have a name property
but I thought that with this "var weatherList = JsonConvert.DeserializeObject<RootObject>(content);" I created a list of Rootobject which contains "name" ?
|
0
var weatherList = JsonConvert.DeserializeObject<RootObject>(content);

I think this line needs to be as follows:

var weatherList = JsonConvert.DeserializeObject<List<RootObject>>(content);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.