0

I doesn't see clearly why the consuming post api failed. it give me this exception while debugging:

[System.out] [OkHttp] sendRequest<< Thread finished: #11 Le thread 0xb s'est arrêté avec le code 0 (0x0).

This is the service:

   public class MachineService : IMachineService
    {
        public  string url = "url";
        public async Task AddMachine(Machine machine)
        {
            try
            {


                HttpClient client = new HttpClient();

                var response = await client.PostAsync(url,
                              new StringContent(JsonConvert.SerializeObject(machine), Encoding.UTF8, "application/json"));

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

and this is the post button method

   private  void Ajout(object sender, EventArgs e)
        {
            SaveMachine();
        }


        public async Task SaveMachine()

        {
            try
            {

                var machine = new Machine
                {
                    Machine_Name = "amira",
                    Machine_Qr = "gazdallah"

                };

                await _rest.AddMachine(machine);

                //await Shell.Current.GoToAsync("..");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Gazdallah Amira
  • 188
  • 3
  • 16
  • That looks like a standard Android log message, not an exception. Are you actually catching an exception? Why do you think this is failing? What is the HTTP response code? – Jason Jun 25 '22 at 19:37
  • it didn't added the model in the database – Gazdallah Amira Jun 25 '22 at 19:50
  • That doesn't mean the request failed. That's why I asked what the HTTP response was. Have you verified that the request reached the server? Did you check the server logs for any problems on that end? – Jason Jun 25 '22 at 19:53
  • it workes fine in swagger and postman and the status code was 200 – Gazdallah Amira Jun 25 '22 at 20:05
  • if your app receives a 200 then you need to look at the server to see why it isn't saving the data – Jason Jun 25 '22 at 20:08
  • I didn't know how to do that in visual studio – Gazdallah Amira Jun 25 '22 at 20:14
  • Did you write the server code? Then you should be able to debug it, or at least add logging to it. If it's a commercial server then you need to see what sort of logging it provides. If this works in Postman, it can generate the C# code needed to make the request and you can compare that to the code you've written. – Jason Jun 25 '22 at 20:17
  • it's SQL server – Gazdallah Amira Jun 25 '22 at 20:22
  • You are calling a webservice, not SQL server directly. Who wrote the web service? – Jason Jun 25 '22 at 20:24
  • I just mention what's the server I used, it's me who wrote the web service and I understand every ligne but honestly I didn't understand your previous comment. Can this [responses](https://stackoverflow.com/questions/18060667/cannot-connect-to-server-a-network-related-or-instance-specific-error) help me for you opinion ? – Gazdallah Amira Jun 25 '22 at 20:32
  • 1
    if your webservice is returning a 200 to the app but not saving any data, then you need to debug the webservice to figure out why. If you don't know how to interactively debug a webservice, there are many tutorials available that explain how. Or you can at least add some basic logging to give you some insight into what the code is doing when it receives your request – Jason Jun 25 '22 at 20:39
  • 1
    The link you provided has to do with when you cannot connect to the server. You told me the server is returning a 200, which means that the client must be able to connect. – Jason Jun 25 '22 at 20:40
  • the 200 status code was in swagger and postman but now I executed the code **Console.WriteLine(ex.Message);** and it returned **InternalServerError** – Gazdallah Amira Jun 25 '22 at 20:57
  • in the very first comment I made **an hour ago** I asked you what the HTTP Response code was. If you had told me that when I asked it would have saved a lot of time. Regardless, a 500 means the request is reaching the server and the server is throwing an exception. So, again, you need to debug your server code. – Jason Jun 25 '22 at 21:01
  • I do some reaserch to how to debug the serer code and I find different methods can you tell me how to do that in your opinion – Gazdallah Amira Jun 25 '22 at 21:27
  • Add some basic exception handling and logging to your code to figure out what is causing the 500. I am not going to walk you through this. – Jason Jun 25 '22 at 21:35

0 Answers0