Last active
August 29, 2015 14:16
-
-
Save csvan/3ee62e0a9511bec44064 to your computer and use it in GitHub Desktop.
Xamarin.Forms JSON client
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
namespace JSONClientExample | |
{ | |
public class JSONClient | |
{ | |
readonly string baseUrl; | |
readonly HttpClient client; | |
public JSONClient (string baseUrl) | |
{ | |
this.baseUrl = baseUrl; | |
this.client = new HttpClient (new NativeMessageHandler ()) { | |
BaseAddress = new Uri (baseUrl), | |
}; | |
} | |
public async Task<MyJSONObject> GetAsync (string url) | |
{ | |
var response = await client.GetAsync (url); | |
response.EnsureSuccessStatusCode (); | |
var rawJSON = await response.Content.ReadAsStringAsync (); | |
var settings = new JsonSerializerSettings { | |
TypeNameHandling = TypeNameHandling.All | |
}; | |
return JsonConvert.DeserializeObject <MyJSONObject> (rawJSON, settings); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment