mercredi 27 décembre 2017

testing web service GET using xamarin forms

I trying to get some basic user information, everything I get is Null , because either no data is coming in or something is missing . any help very much appreciated!

webservice class as follow :

public class InfoRegister

        {

            public string Name { get; set; }

            public string Email { get; set; }


    }  

webservice : is there any way to test this part without render it on a page . force execution or something?

public async Task<List<InfoRegister>> GetRegisterInfo( string token)
        {
            var response = new List<InfoRegister>();

            try
            {
                using (var httpClient = new HttpClient())
                {

                    InitClient(httpClient);

                    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
                    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");

                    var result = await httpClient.GetAsync("http://ift.tt/2bIH98B");

                    if (result.IsSuccessStatusCode)
                    {


                        var data = await result.Content.ReadAsStringAsync();
                        response = JsonConvert.DeserializeObject<List<InfoRegister>>(data);


                        System.Diagnostics.Debug.WriteLine("list" + data);
                    }
                }
                return response;
            }
            catch (Exception ex)
            {

            }

            return response;

        }

view model :

public ObservableCollection<InfoRegister> DisplayRegisterInfo { get; set; }

//here all I see is null , I dont even see the class properties like email or name. public static InfoRegister registerItems;

        public InfoRegister RegisterItems

        {

            get { return registerItems; }

            set

            {

                if (registerItems == value)

                    return;

                registerItems = value;

                OnPropertyChanged(this, new PropertyChangedEventArgs(nameof(RegisterItems)));

            }

        }

page cs:

mydataservice is the page where the webservice call is generated. , the first line is passing the token info and (GetRegisterInfo) is null

 public async  Task LoadInfo ()
            {
     var myInfo = await mydataservice.GetRegisterInfo(Token);
                DisplayRegisterInfo = new ObservableCollection<InfoRegister>(myInfo);


            } 

Aucun commentaire:

Enregistrer un commentaire