Exemple #1
0
        public async void GetJSON()
        {
            //Check network status
            if (!NetworkCheck.IsInternet())
            {
                //await DisplayAlert("JSONParsing", "No network is available.", "Ok");
            }
            else
            {
                var client   = new System.Net.Http.HttpClient();
                var response = await client.GetAsync("REPLACE YOUR JSON URL");

                string contactsJson = await response.Content.ReadAsStringAsync();

                ContactList ObjContactList = new ContactList();
                if (contactsJson != "")
                {
                    //Converting JSON Array Objects into generic list
                    ObjContactList = JsonConvert.DeserializeObject <ContactList>(contactsJson);
                }
                //Binding listview with server response
                string[] ar = new string[ObjContactList.contacts.Count];
                for (int i = 0; i < ObjContactList.contacts.Count; i++)
                {
                    ar[i] = ObjContactList.contacts.ToString();
                }
            }
            //Hide loader after server response
            //ProgressLoader.IsVisible = false;
        }
        private async Task <List <Usuario> > descargarDatosAsync()
        {
            if (NetworkCheck.IsInternet())
            {
                try
                {
                    String url1    = Constantes.cons.URL_BASE + Constantes.cons.URL_GET_ALL_USERS;
                    var    client1 = new HttpClient();
                    Debug.WriteLine("Antes de await1");
                    var response1 = await client1.GetAsync(url1);

                    string result = await response1.Content.ReadAsStringAsync();

                    Debug.WriteLine("eee: " + result);
                }catch (HttpRequestException e)
                {
                    Debug.WriteLine("Excepcion: " + e.Message);
                }
            }


            List <Usuario> usuarios = null;

            Debug.WriteLine("Llega al metodo");
            var client = new HttpClient();

            client.MaxResponseContentBufferSize = 256000;

            Debug.WriteLine("Despeus de crear el objeto cliente");
            //var uri = new Uri(Constantes.cons.URL_BASE + Constantes.cons.URL_GET_ALL_USERS);
            String url = Constantes.cons.URL_BASE + Constantes.cons.URL_GET_ALL_USERS;

            Debug.WriteLine("Despeus de crear uri " + url);
            var response = await client.GetAsync(url);

            Debug.WriteLine("Antes del if");
            //usuarios = JsonConvert.DeserializeObject<List<Usuario>>(response);
            if (response.IsSuccessStatusCode)
            {
                Debug.WriteLine("Dentro del if, respuesta correcta");
                var content = await response.Content.ReadAsStringAsync();

                Debug.WriteLine("Despues del content");
                usuarios = JsonConvert.DeserializeObject <List <Usuario> >(content);

                return(usuarios);
            }

            return(usuarios);
        }