issue on calling api from android emulator

Hameeed jamal 5 Reputation points
2023-09-16T15:39:34.0133333+00:00

i have blazor hybrid maui app i have separate project for api and calling api from hybrid maui app its working fine on window machine but when i run it on android emulator its not working

i run api on http and then try to call its not wokring

here is the method of calling api

     public async Task<string> Login(LoginModel loginModel)
     {
         string ReturnStr = string.Empty;
         using(HttpClient client = new HttpClient())
         {
             BaseUrl = DeviceInfo.Platform == DevicePlatform.Android ? "http://10.0.2.2:5000" : "http://localhost:5000";
             var url = $"{BaseUrl}{ApiUrl.login}";
             var serializedobj=JsonConvert.SerializeObject(loginModel);
           var response=await client.PostAsync(url, new StringContent(serializedobj, Encoding.UTF8, "application/json"));
             if (response.IsSuccessStatusCode)
             {
                 ReturnStr=await response.Content.ReadAsStringAsync();
             }
         }
         return ReturnStr;
     }
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
3,546 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,102 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
1,837 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
8,955 questions
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
240 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 43,906 Reputation points
    2023-09-16T16:46:33.27+00:00

    By default android does not allow insecure traffic (http). You will need to update the manifest to allow http (usesCleartextTraffic)

    or switch to https. See docs for installing certificate.

    0 comments No comments