1

I have a simple Xamarin.android mobile app which is getting data from API hosted in local PC. Problem is when I run the app it will not get the result as expected.

_client is a HttpClient and I'm able to visit the same URL from emulator's browser and get the data. Following is my code.

 protected async override void OnAppearing()
    {
        string content = await _client.GetStringAsync("http://192.168.10.101:1001/api/todo");
        base.OnAppearing();
    }

once I hit F5 emulator is firing up and running the app but it's not getting any data. Also when I debugging, once I hit the F10, await statement will execute an application will continue and will not wait.

Does anyone have any idea about this issue?

I'm using API version 9, level 28 and visual studio 2019 Thanks.

SUNIL DHAPPADHULE
  • 2,755
  • 17
  • 32
MSD88
  • 21
  • 6
  • Can you access http://192.168.10.101:1001/api/todo by brower in your android device or emulate? – Leon May 14 '19 at 07:54
  • @LeonLu-MSFT, yes, i'm able to access the url and get the result in json format from the browser in android emulator – MSD88 May 15 '19 at 04:11
  • Could you share your demo to reproduce this issue? – Leon May 20 '19 at 07:40
  • @LeonLu-MSFT, Seems like the issue is with the permission. I haven't setup the permission or network security config in resources. It gave me an error after few seconds. – MSD88 Sep 25 '19 at 05:07

2 Answers2

1

Issue is with the permission. I haven't setup the permission or network security config in resources. It gave me an error after few seconds. Once i setup those things it worked fine.

MSD88
  • 21
  • 6
0

From the official documentation for await keyword:

The await operator suspends execution until the work of the method is complete. In the meantime, control is returned to the caller. When the task finishes execution, the await expression evaluates to...

What this means is that in some cases (I suspect in your's as well) at the time when the awaited method is executed the program is in a state where this does not matter anymore. If I am correct. Then you can work your way around this by using the Wait() or Result

protected override void OnAppearing()
{
    string content = _client.GetStringAsync("http://192.168.10.101:1001/api/todo").Result;
    base.OnAppearing();
}

PS

However, even if using Result works for you I suggest you to take more time looking into async/await and maybe come up with a better solution to the problem you are solving. This so answer is a good starting point.

Leron
  • 9,546
  • 35
  • 156
  • 257
  • Thanks leron for the answer. But unfortunately, it's not helping, because, await operation should suspend the execution, but my problem is its not happening. Once the await statement has executed, it stop the execution further and exit from the method. Yes, I'm aware of async/await and this is only happening in Xamarin.android environment.Above mentioned code is working as expected in windows and WPF environments. – MSD88 May 09 '19 at 05:44