UWP-058 - UWP Weather - Setup and Working with the Weather API

Sign in to queue

Description

In this second video, Bob gets started by creating the project and focusing on how to retrieve data from the weather API. He demonstrates how to deserialize the JSON returned from the weather service into an object graph of classes created to represent the data structures in the JSON, and also how to use the HttpClient class to call an external website or web service.

Lesson source code: https://github.com/Windows-Readiness/AbsoluteBeginnersWin10/tree/master/UWP-058/UWP-058

Full series source code: https://github.com/Windows-Readiness/AbsoluteBeginnersWin10

PDF: Coming Soon

Embed

Download

Download this episode

Download captions

The Discussion

  • User profile image
    kiko

    Lower half of the screen turned dark after minute 24

  • User profile image
    Adam Hecktman

    I am not even a C# developer (a student in Java right now) and I found this incredibly valuable. Answered some questions I have had for a long time now. Thanks for putting out this series.

  • User profile image
    BobTabor

    @kiko: Hmm... thanks for pointing that out ... I'm not sure what happened there.

  • User profile image
    Clint

    @kiko: Bob made me aware of this, we are uploading a new version, may be an hour before it is corrected

  • User profile image
    Clint

    @kiko: and we're updated with the new version Bob sent over :)

  • User profile image
    TomaszS

    If someone has a problem to call API you may need to add

    &appid=yourKey

    to your url call. You well get your key after registration on OpenWeather.com .

  • User profile image
    zur

    When I run the UWP-058\UWPWeather project and click the Get Weather button, the following code causes and exception.

    RootObject myWeather =  await OpenWeatherMapProxy.GetWeather(
                        position.Coordinate.Latitude,
                        position.Coordinate.Longitude);


    Visual Studio is stating 'Geocoordinate.Latitude.get' is obsolete: Latitude may be altered or unavailable after Windows 8.1. Instead, use Point.Position.

    I have tried adding my own key (&appid=yourKey) to the end of var response = await http.GetAsync("http://api.openweathermap.org/data/2.5/weather?lat=32.77&lon=-96.79&units=imperial&appid="***myKey***"); on the OpenWeatherMapProxy class and that did not solve the problem.

    Changing the code to the following also did not work.

    Geoposition position = await LocationManager.GetPosition();
    await OpenWeatherMapProxy.GetWeather(
                        position.Coordinate.Point.Position.Latitude,
                        position.Coordinate.Point.Position.Longitude);

    Does anyone know how to solve this exception.

  • User profile image
    Hemanzt

    For me the sample code snippet doesn't work. Any one else with the same problem?

     

  • User profile image
    BobTabor

    @zur: When you say "Visual Studio is complaining" ... Yes, Visual Studio will complain.  While that API has been deprecated, at the time of this recording there was no replacement that I was aware of.  The key is: does it COMPILE and run?  (It should ... that's my exact code).

    @Hemanzt: Could you be more specific?  I'll help if I can.

  • User profile image
    Ted Driver

    Bob,
    Are you aware that Visual Studio will do the JSON to C# conversion for you? Just copy the JSON as you did, and then create a new class file in Visual Studio. Then select Edit Menu|Paste Special|Copy JSON as classes. Works for XML too.

  • User profile image
    BobTabor

    @Ted Driver: Doh!  Yes, now that you mention it I have heard that but clearly got stuck in my old ways.  Thank you!  I'll try to remember that next time.

  • User profile image
    ToyMaker

    Why on earth use the DataContractJsonSerializer and oblige yourself to write all this code and datacontract/datamember decorations (ugly stuff), and NOT use Newtonsoft.Json in one line (return JsonConvert.DeserializeObject<RootObject>(result);)? It's become industry standard nowadays. :)

    Still, great stuff Bob. Thanks a bunch for your effort.
    Great point Ted, didn't know this special paste function even existed. ;)

  • User profile image
    WayneHoggett

    Thanks again for a great video Bob.

    ToyMaker, that is a great tip. You can replace 3 lines of code with just one.

                //var serializer = new DataContractJsonSerializer(typeof(RootObject));
                //var ms = new MemoryStream(Encoding.UTF8.GetBytes(result));
                //var data = (RootObject)serializer.ReadObject(ms);

    Replaced with:

    var data = JsonConvert.DeserializeObject<RootObject>(result);

     

     

     

  • User profile image
    itguru

    I have to abandon yet again another try at coding. Followed it exactly.

    'System.Runtime.Serialization.Json.DataContractJsonSerializer' is a 'type' but is used like a 'variable'

    what does that mean??

    DataContractJsonSerializer

  • User profile image
    golkhandani

    How Can I use my own txt file with json format to add data like:

    {"city":"London","description":"overcast clouds",....}

    instead of using these

    var http = new HttpClient();
    var response =await http.GetAsync(string.Format(""));
    var result = await response.Content.ReadAsStringAsync();

    actualy how can I use txt file for data?

     

  • User profile image
    RobertLouis
    When i click the button i have the following error : NullReferenceException at the ResultTextBlock step.



    private
     async void Button_Click(object sender, RoutedEventArgs e)

    RootObject myWeather = await OpenWeatherMapProxy.GetWeather(20.0, 30.0);
    ResultTextBlock.Text = myWeather.name + " - " + ((int)myWeather.main.temp).ToString() + " - " + myWeather.weather[0].description;
    }

  • User profile image
    YetkinAkin

    some of properties should be double. Such as "deg". Because the deg is decimal. That was the error for me.
    And the other thing; in the url, we should have an APPID
    http://api.openweathermap.org/data/2.5/weather?lat=32.77&lon=-96.79&APPID=xxxxxxxxxxxxxx&units=imperial

  • User profile image
    IamThePeter

    @ToyMaker:

    Yeah,... I was typing along and thinking, I don't normally have to do all this. ;)

    As a basic example, it's probably okay. If you were just starting out I can see why Bob would use it.

  • User profile image
    Cyberlacs

    @BobTabor how can I indent gauge in this application, is there any library?

Add Your 2 Cents