1

I am trying to grab some values from my web api using HttpClient. I managed to get a true status. However, I do not know how to grab values/read the JSON document. May I know if there's a way to do it?

I am currently doing in Xamarin.Forms in Visual Studio.

This is my code.

When I enter this URL into my browser, the document reads like this

{"d":[{"__type":"Info:#website.Model","infoClosingHours":"06:00:00 PM","infoID":1,"infoOpeningDays":"Monday","infoOpeningHours":"09:00:00 AM","infoStatus":"Open"}]}

xaml file

<Button Text="Grab Value" Clicked="GetData"/>

xaml.cs file

private void GetData(object sender, EventArgs e)
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("ipaddress");

            // Add an Accept header for JSON format.
            client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));

            try{
                HttpResponseMessage response = client.GetAsync("WebServices/information.svc/GetInformationJSON").Result;
                HttpResponseMessage response1 = client.GetAsync("WebServices/information.svc/GetInformationJSON").Result;
            }
            catch
            {

            }

        }
GCK 1617
  • 89
  • 1
  • 2
  • 10

2 Answers2

3

I recommend using a static HttpClient if you will have any kind of decent load on your app. Otherwise, you can experience port exhaustion and bring the server to its knees. See my response on using instance-based vs. static HttpClients - What is the overhead of creating a new HttpClient per call in a WebAPI client?

Community
  • 1
  • 1
Dave Black
  • 7,305
  • 2
  • 52
  • 41
0

You could use it like this:

private async void GetData(object sender, EventArgs e)
{
    using (HttpClient client = new HttpClient())
    {
        client.BaseAddress = new Uri("ipaddress");

        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        try
        {
            HttpResponseMessage response = client.GetAsync("WebServices/information.svc/GetInformationJSON").Result;
            if (response.IsSuccessStatusCode)
            {
                MyObject responseObject = response.Content.ReadAsAsync<MyObject>();
            }
        }
        catch
        {

        }
    }
}

For this to work you need to create a class "MyObject" that has the Properties from your JSON-Data.

It would also be possible to just deserialize it to a dynamic object like this:

private async void GetData(object sender, EventArgs e)
{
    using (HttpClient client = new HttpClient())
    {
        client.BaseAddress = new Uri("ipaddress");

        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        try
        {
            HttpResponseMessage response = client.GetAsync("WebServices/information.svc/GetInformationJSON").Result;
            if (response.IsSuccessStatusCode)
            {
                string jsonString = await response.Content.ReadAsStringAsync();
                dynamic dynamicObject = JsonConvert.DeserializeObject(jsonString);
            }
        }
        catch
        {

        }
    }
}

For this you would need the Newtonsoft.Json.

Shamshiel
  • 2,051
  • 3
  • 31
  • 50
  • We actually use Newtonsoft.Json to parse it to a known type. It's like combining the two code snippets into one but I think it results in more forgiving parsing but with a usable type. – No Refunds No Returns Apr 04 '17 at 16:41