My app aborts when trying to consume Rest

Asked

Viewed 113 times

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 turns out he aborts the moment he consumes the Service. That’s the code I have for consuming 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 when you get on 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 that line and does not fall in the catch. It aborts the method altogether. What can it be? Someone has been there 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 you get on that line: response = await client.PostAsync(uri, content);

Enter Exception. The message is:

Object Reference not set to an instance of an Object.

I do not know which object is null. My distrust is whether I really need to instantiate the Service, since it is a WCF. I don’t see anything I haven’t instantiated.

EDIT2

the message

Object Reference not set to an instance of an Object.

I already solved it. It’s just that client was declared at the beginning of the class, but not instantiated. Solved. Now when you get here HttpResponseMessage response = await client.PostAsync(uri, content);

No mistake, but it comes out of the loop. Since it’s a post, is this the normal behavior or not? How do I test a post.

EDIT3

I managed to stop on break now. The mistake? Yes, it again:

Object Reference not set to an instance of an Object.

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

EDIT4

in fact the mistake of

Object Reference not set to an instance of an Object.

It has to do with the call button Try.. catch and not the method, as Listview is not tied to the service result gives this error. What happens is that I made a console application to test and gives the same thing, IE, simply abort while trying to consume the service. I’ll do a service here Web API and see if it has anything to do with the technology used to build the services. I just need to make sure the way I did is correct or not.

EDIT5

I’m sure the problem is in class HttpClient, because I made a console and I couldn’t consume the Service. So I installed the Restsharp class and managed to consume the service. The problem is that Restsharp cannot install in Xamarin.Forms. I even opened a thread about it here at Sopt.

  • Pole to exception which is occurring, if it doesn’t get complicated to help

  • @Lmaker, no Exception. He doesn’t catch and when he gets to the quoted line, he simply aborts. I don’t know how to force show an Exception.I’ll put a Try.. catch on the call button and see if there it shows some Exception. I’ll be right back.

  • @Lmaker, I put a Try.. catch on the call and I don’t catch. Abort and it looks like it’s working, but not in debug I can’t go all the way in the Service method.

  • Just to test, take the await and put the . Result at the end

  • @Leandroangelo, I did this and when it comes to Result. It gave Canceled status. I think the approach to consume Rest with Post was not the best. I need to find another way.

  • @pnet don’t think this problem is happening because you are running In debug mode.

  • Hello @Paulohdsousa, too I think, but until the moment of the comment I had this suspicion. I made an issue in the post stating what else I could walk, but still no solution.

  • @Lmaker, the Exception is Object reference not set to an instance of an object.

  • This error says you are referencing some object that is null.

  • @Lmaker, thank you for answering. But I’m redoing everything I did and I have nothing null or unspecified. The service, passing its URL through the SOAP UI it returns something. It is a REST service and still in doubt about it.

Show 5 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.