admin管理员组

文章数量:1279021

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(";).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.

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./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.

Share Improve this question asked Feb 24 at 14:54 Chad McCallumChad McCallum 211 silver badge2 bronze badges 1
  • https://httpbin./get does not appear to be a local link, so no special settings are required on the Android device. For how the Maui program obtains data from the webservice, you can refer to the following official documents and examples. Consume a REST-based web service. – YonglunLiu Commented Feb 25 at 1:29
Add a comment  | 

1 Answer 1

Reset to default 2

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.

本文标签: NET Maui on Android Emulator not running httpclientgetasyncStack Overflow