Forum General

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

How to call a get REST API from a tvOS Application using Xamarin?

SreeeeSreeee INMember ✭✭✭✭✭

I am trying to call a get REST API from my tvOS application. Following is my code when tap the Button:

async void ButtonClicked(UIButton sender)
        {
            try
            {
                HttpClient client = new HttpClient();
                var response = await client.GetAsync("rest api url");
                if (response.IsSuccessStatusCode)
                {
                    var Response = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync());
                    if (!string.IsNullOrWhiteSpace(Response.ToString()))
                    {
                        var category = JsonConvert.DeserializeObject<Videos>(Response.ToString());
                        Debug.WriteLine("count:>>" + category.webContentCategoryList.Count);
                    }
                }
            }
            catch(Exception e)
            {
                Debug.WriteLine("Exception:>>"+e);
            }

I have installed the system.net.http and newtonsoft.json nuget packages. But when I run the project the application showing Main.cs file like below screenshot:

Am I missing something in this?

Tagged:

Best Answer

  • SreeeeSreeee INMember ✭✭✭✭✭
    Accepted Answer

    @JohnH I have fixed this by separating the service call on a new function like below, new function is the async method:

        partial void ButtonClicked(UIButton sender)
            {
                LoadData();
            }
    
            async void LoadData()
            {
                HttpClient client = new HttpClient();
                var response = await client.GetAsync("service url");
                if (response.IsSuccessStatusCode)
                {
                    var Response = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync());
                    if (!string.IsNullOrWhiteSpace(Response.ToString()))
                    {
                        var category = JsonConvert.DeserializeObject<Videos>(Response.ToString());
                        Debug.WriteLine("count:>>" + category.Count);
                    }
                }
            }
    

Answers

  • JohnHJohnH GBMember ✭✭✭✭✭
    Have you identified what line of code causes the crash? Try stepping through the code to see.
  • SreeeeSreeee INMember ✭✭✭✭✭

    @JohnH I have added breakpoint for the first line inside ButtonClicked function. When I taps the Button, the application showing Main.cs file like above screenshot. It is not hitting the first line of ButtonClicked function.

    So the issue is something else, I am not an expert in tvOS applications so I can't figure out. I have uploaded a sample project here. Could you please have a look?

  • JohnHJohnH GBMember ✭✭✭✭✭

    Have you tried commenting out all the code in the ButtonClicked event, does it still crash?

  • SreeeeSreeee INMember ✭✭✭✭✭
    Accepted Answer

    @JohnH I have fixed this by separating the service call on a new function like below, new function is the async method:

        partial void ButtonClicked(UIButton sender)
            {
                LoadData();
            }
    
            async void LoadData()
            {
                HttpClient client = new HttpClient();
                var response = await client.GetAsync("service url");
                if (response.IsSuccessStatusCode)
                {
                    var Response = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync());
                    if (!string.IsNullOrWhiteSpace(Response.ToString()))
                    {
                        var category = JsonConvert.DeserializeObject<Videos>(Response.ToString());
                        Debug.WriteLine("count:>>" + category.Count);
                    }
                }
            }
    
  • SreeeeSreeee INMember ✭✭✭✭✭
    edited June 2020

    @JohnH How can I do the above feature without a new function named LoadData? (I mean inside of ButtonClicked function).

    I have done like below but finally found a problem:

    I have changed the function ButtonClicked to async because of the use of await keyword like below:

    async void ButtonClicked(UIButton sender)
            {
                HttpClient client = new HttpClient();
                var response = await client.GetAsync("service api");
                if (response.IsSuccessStatusCode)
                {
                    var Response = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync());
                    if (!string.IsNullOrWhiteSpace(Response.ToString()))
                    {
                        var category = JsonConvert.DeserializeObject<Videos>(Response.ToString());
                        Debug.WriteLine("count:>>" + category.Count);
                    }
                }
            }
    

    After this change I found an error on ViewController.designer.cs file.

    How can I fix this type of problems? :o

    I have deleted the code on ViewController.designer.cs file and run the project early, that's why I got such error.

Sign In or Register to comment.