.net MAUI class not working correctly on Android Device or Android Simulator.

Heinz Deubler 181 Reputation points
2023-03-02T03:42:25.2433333+00:00

The class below is from a .net 7.0 MAUI app and it retrieves data from a REST API.

When I run the program from "Windows Machine" it works fine. However, when I run the program from and android simulator or Android phone it doesn’t work.

I call the method “GetPilots()” which then call “GetToken()” to get a token from the RESTApi. This part works fine. I verified that the has been gotten successfully.

The method continues executing and at line “RestResponse response = client.Execute(request);” I get the following the following error "Method Not Allowed" at line"

 public class PilotSQLServerRepository 
    {
        public string GetToken() {
             var client = new RestClient("https://<domain>/");
            var request = new RestRequest("Login", Method.Post);

            request.AddHeader("Content-Type", "application/json");
            request.AddParameter("application/json", ParameterType.RequestBody);
            RestResponse response = client.Execute(request);
             var result = (JObject)JsonConvert.DeserializeObject(response.Content);
             var token = result["result"].Value<string>();

            return token;
        }

        public void GetPilots() {

            string token = GetToken();
            var client = new RestClient("https://<domain>/");
            try {
                var request = new RestRequest("Pilot", Method.Get);
                request.AddHeader("Authorization", "bearer " + token.Trim((char)34));
                var body = @"";
                request.AddParameter("text/plain", body, ParameterType.RequestBody);

                RestResponse response = client.Execute(request);       // On and android phone or simulator this line returns
                                                                       // METHODE NOT ALLOWED. On a Windows Machine the
                                                                       // the class returns the correct result from the RESTApi
                var pilot = JsonConvert.DeserializeObject<List<Pilot>>(response.Content);
               // Console.WriteLine("Code = " + response.StatusCode);
               Console.ReadKey();
                
              //  var result = await Task.FromResult(new List<Pilot>());
            }
            catch (Exception ex) {
               
            }
        }
    }
Developer technologies | .NET | .NET MAUI
Developer technologies | C#
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Heinz Deubler 181 Reputation points
    2023-03-03T05:04:26.8666667+00:00

    I figured it out. I commented this line and it worked. To be honest I have no idea what this line of code does.

    request.AddParameter("text/plain", body, ParameterType.RequestBody);

    0 comments No comments

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.