Skip to content

Instantly share code, notes, and snippets.

@csvan
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csvan/3ee62e0a9511bec44064 to your computer and use it in GitHub Desktop.
Save csvan/3ee62e0a9511bec44064 to your computer and use it in GitHub Desktop.
Xamarin.Forms JSON client
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