Reputation: 21
Visual Studio 2022, installed the .NET Maui toolkit. File -> New Project with .NET Maui Blazor Hybrid App template.
Open up Home.razor and change to the following:
@page "/"
<h1>Hello, world!</h1>
Welcome to your new app.
<button @onclick="FetchData">Fetch</button>
<div>@data</div>
@code
{
private string data = "";
private void FetchData()
{
var client = new HttpClient();
var response = client.GetAsync("https://httpbin.org/get").Result;
data = response.Content.ReadAsStringAsync().Result;
}
}
Put a breakpoint on the client.GetAsync
call
Run the app with the android emulator (Pixel 5, API 34).
Click the 'Fetch' button, breakpoint hits in visual studio.
Click "Step Over" in the debug toolbar.
Nothing happens. No error message, no next line, nothing.
I attached with the chrome devtools debugger, and no network call is ever made.
I also tried with an async Task FetchData
method, no difference.
What am I doing wrong here?
---
Edit: I have tried
injecting HttpClient
using an async method with @onclick="@FetchData"
adding ConfigureAwait(false)
ensuring the android manifest file has the right permissions (INTERNET, ACCESS_NETWORK_STATE, ACCESS_WIFI_STATE)
setting android:usesCleartextTraffic=true
calling the FetchData method in OnAfterRenderAsync
and OnInitializedAsync
using .NET 9
None of these changes did anything - they all behave the same way.
Upvotes: 0
Views: 33
Reputation: 21
After doing even more searching, I tried getting the emulator to reboot (not just saving state and shutting down), and lo and behold that worked. After the emulator restarted, it was able to make API calls over https.
Leaving this here in cause anyone else runs into the same issue.
Upvotes: 2