issue on calling api from android emulator

Hameeed jamal 10 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.
4,382 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,495 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,206 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.
10,624 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.
317 questions
{count} vote

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 61,181 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.

    1 person found this answer helpful.
    0 comments No comments

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more