skip to Main Content

I am developing and app that consume APIs (Using genymotion emulator for testing), I´ve made a simple test content page in which behind code I have the next:

using PlaqueoApp.Modelos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using System.Net.NetworkInformation;
using Newtonsoft.Json;

namespace PlaqueoApp
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class RegistrarPatron : ContentPage
    {
        public RegistrarPatron ()
        {
            InitializeComponent ();

            GetOficinas().Wait();

        }

        private async Task GetOficinas()
        {
            Ping myPing = new Ping();
            PingReply reply = myPing.Send("8.8.8.8", 10000);


            HttpClient cliente = new HttpClient();

            string url = "https://reqres.in/api/users?page=2";

            var response = await cliente.GetStringAsync(url);

            var anonimus = JsonConvert.DeserializeObject<List<object>>(response);


        }
    }
}

My problem is that, when it reaches GetStringAsync it last forever, I mean, the method call never returns and I have to stop it.
This is what the response should look like:

{
  "page": 2,
  "per_page": 3,
  "total": 12,
  "total_pages": 4,
  "data": [
    {
      "id": 4,
      "email": "[email protected]",
      "first_name": "Eve",
      "last_name": "Holt",
      "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/marcoramires/128.jpg"
    },
    {
      "id": 5,
      "email": "[email protected]",
      "first_name": "Charles",
      "last_name": "Morris",
      "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/stephenmoon/128.jpg"
    },
    {
      "id": 6,
      "email": "[email protected]",
      "first_name": "Tracey",
      "last_name": "Ramos",
      "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/bigmancho/128.jpg"
    }
  ]
} 

I thought it was because Iternet connection but I checked Gennymotion emulator, and WIFI option is enable. I also made a ping to google as you can see and I get Success status, I also add Internet permission on Manifest.
I don´t know if I am missing something, if is something on Debugging my app or anything else

2

Answers


  1. Modify the timeout on the HttpClient named cliente before making the request and add a try/catch block around the request to see what’s going on.

    If you have a separate async method that makes the call, you can put the try/catch in there and then call the separate method. Otherwise, Call .Wait() on the task before wrapping exception handling around it. The Wait() would be for testing purposes and not production use.

    Login or Signup to reply.
  2. Don’t do this:

            public RegistrarPatron ()
            {
                InitializeComponent ();
    
                GetOficinas().Wait();
            }
    

    You should almost never Wait() on a Task like this: it will freeze your app or block a thread forever.

    Do this instead:

    namespace PlaqueoApp
    {
        [XamlCompilation(XamlCompilationOptions.Compile)]
        public partial class RegistrarPatron : ContentPage
        {
            public RegistrarPatron ()
            {
                InitializeComponent ();
            }
    
            protected override async void OnAppearing()
            {
                try 
                {
                    await GetOficinas();
                }
                catch (Exception exception)
                {
                    Debug.WriteLine(e.ToString());
                }   
            }
    
            private async Task GetOficinas()
            {
                Ping myPing = new Ping();
                PingReply reply = myPing.Send("8.8.8.8", 10000);
    
                HttpClient cliente = new HttpClient();
    
                string url = "https://reqres.in/api/users?page=2";
    
                var response = await cliente.GetStringAsync(url);
    
                var anonimus = JsonConvert.DeserializeObject<List<object>>(response);
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search