PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, September 28, 2022

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

 September 28, 2022     rest, tvos     No comments   

Issue

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?

UPDATE

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.


Solution

I have fixed this issue 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);
            }
        }
    }

My XF Thread is here for more details.



Answered By - Sreejith Sree
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing