Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Donghuan-He/RestService.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
34 lines (30 sloc)
866 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json; | |
using System.Diagnostics; | |
namespace WeatherApp | |
{ | |
public class RestService | |
{ | |
HttpClient _client; | |
public RestService() | |
{ | |
_client = new HttpClient(); | |
} | |
public async Task<WeatherData> GetWeatherData(string query) { | |
WeatherData weatherData = null; | |
try | |
{ | |
var response = await _client.GetAsync(query); | |
if (response.IsSuccessStatusCode) { | |
var content = await response.Content.ReadAsStringAsync(); | |
weatherData = JsonConvert.DeserializeObject<WeatherData>(content); | |
} | |
} | |
catch (Exception ex) | |
{ | |
Debug.WriteLine(ex.Message); | |
throw; | |
} | |
return weatherData; | |
} | |
} | |
} |