My app aborts while trying to consume rest

0

It has a rest service that my android app needs to consume. For this I have to send a parameter in json format. It happens that it aborts the moment it consumes the Service . This is the code I have to consume the Service :

public async Task<string> PostIndicador(string jsonValue)
        {
            string retorno = null;
            if (NetworkCheck.IsInternet())
            {
                string url_base = $"meu_ip";
                var uri = new Uri(string.Format(url_base));
                using (var client = new HttpClient() { BaseAddress = uri })
                {
                    var responeMessage =
                    await client.PostAsync("/Service/Service.svc/GetIndicator", new StringContent("jsonValue", Encoding.UTF8, "application/json"));

                    var resultcontent = await responeMessage.Content.ReadAsStringAsync();
                    retorno = (JsonConvert.DeserializeObject(resultcontent)).ToString();
                }                
            }

            return retorno;
        }

The call code is this:

private void Click_Service(object sender, EventArgs e)
        {
            DataService dataService = new DataService();
            string jvalue = "{"\"Matriz\":12, \"Filial\":21"}";
            dataService.PostIndicador(jvalue);
        }     

It turns out that when you get to this line

var responeMessage = await client.PostAsync("/Service/Service.svc/GetIndicator", new StringContent("jsonValue", Encoding.UTF8, "application/json"));

The system aborts. I put a try..catch only for this line and it does not fall into the catch. It aborts the method completely. What can it be? Has anyone experienced this and wants to share the experience?

EDIT1

I changed my method for this new approach

public async Task PostIndicador(IndicadorPost indicador)
        {
            try
            {
                string url_base = $"http://meu_ip/Service/Service.svc/GetIndicator";
                var uri = new Uri(string.Format(url_base));
                var data = JsonConvert.SerializeObject(indicador);
                var content = new StringContent(data, Encoding.UTF8, "application/json");
                HttpResponseMessage response = null;
                response = await client.PostAsync(uri, content);

                //if (!response.IsSuccessStatusCode)
                //{
                //    throw new Exception("Erro ao atualizar tabela de liberacao");
                //}
            }
            catch (Exception ex)
            {
                string er = ex.Message;
            }
        }

Now when it comes to this line: response = await client.PostAsync(uri, content);

Enter the exception. The message is:

  

Object reference not set to an instance of an object.

I do not know which object is null. My mistrust is if I really need to instantiate the Service, since it is a WCF . Since it was not, I do not see anything that I did not instantiate.

EDIT2

the message

  

Object reference not set to an instance of an object.

I have already resolved. The client was declared at the beginning of the class, but not instantiated. Sorted out. Now when it arrives here HttpResponseMessage response = await client.PostAsync(uri, content);

It does not make any mistake, but it comes out of the loop. As a post, is this normal behavior or not? How do I test a post.

EDIT3

I was able to stop on the break now. The mistake? Yes, it again:

  

Object reference not set to an instance of an object.

This is the error you are giving at the time of consuming the service. If it can not connect to the service, can this bug explode in the App? What else can be null or not instantiated? Do I need to instantiate the Service (WCF)? Those are the questions I have.

EDIT4

Actually the error of

  

Object reference not set to an instance of an object.

It has to do with the try..catch of the call button and not the method, as the Listview is not tied to the result of the service gives this error. What happens is that I did a console application to test and it gives the same thing, ie it simply aborts when trying to consume the service. I will do a service here Web API and see if it has something to do with the technology used to build the services. I just need to make sure the way I did it is correct.

EDIT5

I'm sure the problem is in the HttpClient class because I made a Console and could not consume the Service . So I installed the RestSharp class and managed to consume the service. The problem is that RestSharp can not install in Xamarin.Forms. I even opened a thread about it here in SOpt.

    
asked by anonymous 11.01.2018 / 19:43

0 answers