Skip to content

SyncfusionExamples/fetch-data-using-webapi-treeview-xamarin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 

Repository files navigation

fetch-data-using-webapi-treeview-xamarin

This repository showcases how to populate the Syncfusion Xamarin.Forms TreeView control with hierarchical data fetched from a Web API. The included sample demonstrates making HTTP requests to retrieve JSON data, deserializing the response into a suitable object hierarchy, and binding the result to the TreeView.

Sample

Helper

internal class WebApiServices
    {
        public static string webApiUrl = "Your URL"; // Your Web Api here

        System.Net.Http.HttpClient client;

        public WebApiServices()
        {
            client = new System.Net.Http.HttpClient();
        }

        public async System.Threading.Tasks.Task<ObservableCollection<RootTree>> RefreshDataAsync()
        {
            var uri = new Uri(webApiUrl);
            try
            {
                //Sends request to retrieve data from the web service for the specified Uri
                var response = await client.GetAsync(uri);

                if (response.IsSuccessStatusCode)
                {
                    var content = await response.Content.ReadAsStringAsync(); //Returns the response as JSON string
                    return JsonConvert.DeserializeObject<ObservableCollection<RootTree>>(content); //Converts JSON string to ObservableCollection
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(@"ERROR {0}", ex.Message);
            }

            return null;
        }
    }

Requirements to run the demo

To run the demo, refer to System Requirements for Xamarin

Troubleshooting

Path too long exception

If you are facing path too long exception when building this example project, close Visual Studio and rename the repository to short and build the project.

About

How to fetch data using Web API in Xamarin.Forms TreeView

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 6

Languages