5

I have this simple method which suppose to get weather data, when I call it this error occur:

System.Runtime.Serialization.SerializationException was unhandled by user code HResult=-2146233076 Message=There was an error deserializing the object of type UWpWeather.RootObject. Encountered unexpected character '<'.

public async static Task <RootObject> GetWeather(double lat, double lng) {
    var http = new HttpClient();
    var response = await http.GetAsync("http://api.openweathermap.org/data/2.5/forecast/daily?q=leeds&type=accurate&mode=xml&units=metric&cnt=3&appid= MY AIP-KEY");
    string result = await response.Content.ReadAsStringAsync();
    var serializer = new DataContractJsonSerializer(typeof (RootObject));
    var ms = new MemoryStream(Encoding.UTF8.GetBytes(result));
    var data = (RootObject) serializer.ReadObject(ms);
    return data;
}
Yannick Meeus
  • 5,643
  • 1
  • 35
  • 34
khalefa
  • 77
  • 2
  • 11

2 Answers2

1

The API does not honour any of the HTTP content or Accept headers you pass through on the request, but rather it sets the content-type of the response based on the query string parameter.

Your initial URL:

http://api.openweathermap.org/data/2.5/forecast/daily?q=leeds&type=accurate&mode=xml&units=metric&cnt=3&appid= MY AIP-KEY"

What it should be:

http://api.openweathermap.org/data/2.5/forecast/daily?q=leeds&type=accurate&mode=json&units=metric&cnt=3&appid= MY AIP-KEY"

That should allow you to deserialize it into your RootObject correctly.

Caveat: I don't have your root object implementation, so I could only verify up until getting a JSON-formatted response back.

Yannick Meeus
  • 5,643
  • 1
  • 35
  • 34
  • Yannick Meeus, thanks. I changed my code as you said and now I got another error: Expecting state 'Element'.. Encountered 'Text' with name ", namespace". BTW, when put that address in firefox I get weather data as expected. – khalefa Apr 29 '17 at 18:37
  • 1
    That's more than likely to do with your RootObject not being set up correctly. Can you post your RootObject implementation? The way I tend to generate these is to "Paste Special - Paste JSON as Classes", once I've copied an example JSON response. That pretty much guarantees I'll be able to deserialize correctly. I've just run your code, with my generated RootObject and it's all working fine, which is why I'm guessing it's your RootObject. I'll be flying for the next 16 hours, but if you can't get it fixed by then I can have another look. – Yannick Meeus Apr 29 '17 at 21:05
  • I'm interested to see you code as it may help in other situations. I'm calling my method like this: RootObject myWeather = await OpenWeather.GetWather(12.2, 32.0); – khalefa Apr 29 '17 at 21:17
0

I found the answer, my first mistake was using Xml instead of Json when calling my data. second, when I used this website (json2csharp) to convert Json to series of classes that represent my Json it created it fine except one which created as a list public List<List> list { get; set; } I simply removed that one and my code now is working just fine. thanks all for your support.

khalefa
  • 77
  • 2
  • 11