.NET MAUI Android - System.PlatformNotSupportedException

Yusuf-3141 70 Reputation points
2025-02-19T18:47:54.31+00:00

Hi,
I'am trying to run my .NET MAUI project on Android, but I'm getting the following error:

System.PlatformNotSupportedException Message=Operation is not supported on this platform.

Here is the project link:
https://github.com/devenv2/SmartChat/blob/master/SmartChat/MainPage.xaml.cs

Has anyone encountered this issue before? Any ideas on how to fix it?

Thanks in advance

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,942 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 80,101 Reputation points Microsoft Vendor
    2025-02-20T03:24:54.1166667+00:00

    Hello,

    This issue could be reproduced in android platform.

    Based on this Tutorial: Create a recommendation app with .NET MAUI and ChatGPT, it will create a .NET MAUI app for **Windows ** in Visual Studio that calls OpenAI's ChatGPT APIs, android platforms do not include it.

    You can open an issue in the openai dotnet, confirm with PG about will this nuget package support for android platform.

    However, you can call REST API by HttpClient to do it.

     var apiKey = "sk-xxxxxxxxxxxxx"; // input your API Key
     var endpoint = "https://api.openai.com/v1/chat/completions";
    
     var httpClient = new HttpClient();
     httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
    
     var requestBody = new
     {
         model = "gpt-3.5-turbo", 
         messages = new[]
         {
         new { role = "user", content = "Say a quote" }
     }
     };
    
     var requestContent = new StringContent(
     JsonSerializer.Serialize(requestBody),
     Encoding.UTF8,
     "application/json"
     );
    
     var response = await httpClient.PostAsync(endpoint, requestContent);
     var responseBody = await response.Content.ReadAsStringAsync();
    
     Console.WriteLine(responseBody);
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.