Prissy Eve
Prissy Eve

Reputation: 156

How to use singleton class for get and Post the json values in xamarin forms?

I developing a xamarin application using singleton based to get the server values. Searched the example by StackOverflow in this site(Show List in Xamarin consuming REST API). but I have small doubts about this. no one reply to my comment to this question.

My Code:

public async Task<Response> GetList<T>(string urlBase, string servicePrefix, string controller)
    {
        try
        {
            var client = new HttpClient();          
            client.BaseAddress = new Uri(urlBase);
            var url = string.Format("{0}{1}", servicePrefix, controller);
            var response = await client.GetAsync(url);
            var result = await response.Content.ReadAsStringAsync();

            if (!response.IsSuccessStatusCode)
            {
                return new Response
                {
                    IsSuccess = false,
                    Message = result,
                };
            }

            var list = JsonConvert.DeserializeObject<List<T>>(result);

            return new Response
            {
                IsSuccess = true,
                Message = "Ok",
                Result = list,
            };
        }
        catch (Exception ex)
        {
            return new Response
            {
                IsSuccess = false,
                Message = ex.Message,
            };
        }
    }

My doubt what is Response in this code. It is a separate class or HTTP response message. but I changed the HTTP response message, it gives an error in the above-declared variables(Success, Message).

Upvotes: 0

Views: 159

Answers (1)

Leon Lu
Leon Lu

Reputation: 9234

Response is a Custom class like following code,.It contails three properties.

    public class Response
    {
        public bool IsSuccess { get; set; }
        public string Message { get; set; }
        public object Result { get; set; }
    }

Upvotes: 1

Related Questions