locked
connect Xamarin to rest web service RRS feed

  • Question

  • User394026 posted

    hi, I'm starting learning Xamarin and i face problem in connecting app with DB. i built rest web services with method get and i uplode it in host. after that i add this code in app public MainPage() { var mylist = GetlistAsync(); InitializeComponent(); } private async Task> GetlistAsync() { var httpClient = new HttpClient(); var response = await httpClient.GetAsync("my url"); List Items = new List(); var content = await response.Content.ReadAsStringAsync(); Items = JsonConvert.DeserializeObject>(content); return Items; } the problem is whenever i add run. the code will move to InitializeComponent after httpclint. getasync line without doing lines after it. so i test if verified connectivity between your device/emulator and your server and it work . after that i tray to test the code in web application mvc and it works fine but when i tray it in xamarin form app it doesn't work . pleas help as fast as possible thanks

    Monday, April 20, 2020 10:12 PM

All replies

  • User185808 posted

    I'm not sure but I think var mylist = GetlistAsync(); need to go below the InitializeComponent(); Futhermore, your GetlistAsync is async so you show await it, otherwise, nothing will ever be returned.

    Tuesday, April 21, 2020 5:26 AM
  • User394026 posted

    i do as u say and still there is no response from httpclint

    Tuesday, April 21, 2020 8:19 PM
  • User394026 posted

    i was trying to use break point to figure out what is the problem but it did not return any problem

    Tuesday, April 21, 2020 9:14 PM
  • User385166 posted

    For async

    Must task.run() https://www.pluralsight.com/guides/using-task-run-async-await

    Wednesday, April 22, 2020 5:10 AM
  • User185808 posted

    There's no need for a task run to get this working. You can try

    var mylist = GetlistAsync().GetAwaiter().GetResult();

    But you should be aware that calling an API on UI Thread will cause you App to freeze. To remedy this, you'll need to run this in the background (i.e Taks.Run)

    var mylist = Task.Run(() => CachePages(Page.Progress.Issue)).GetAwaiter().GetResult();

    Thursday, April 23, 2020 7:09 AM