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
            {

            }

        }

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.