Pass json format parameter to consume service post in app Xamarin.Forms

Asked

Viewed 503 times

0

In my App I have this method to consume the service post (still writing the method):

public async Task<string> PostIndicador(string jsonValue)
{
    client = new HttpClient();
    string url = $"http://meu_ip/indicadorservice/indicadorservice.svc/metdodoservico";
    HttpContent content = new StringContent(jsonValue, Encoding.UTF8, "application/json");
    var uri = new Uri(string.Format(url));
    var response = await client.PostAsync(uri, content);

    string responseMessage = await response.Content.ReadAsStringAsync();
    return responseMessage;
}

The question is: How do I pass the parameter in json format for this method? The parameter comes in the argument jsonValue.

A clarification only: I must pass the method in the URL Service(metdodoservico)?

P.S. Another company developed the Service, so I do not have access to nothingness.

EDIT1

My model

public class TipoIndicador
    {
        public int IDUsuario { get; set; }
        public int IdTipoIndicador { get; set; }
        public decimal ValorPrevisto { get; set; }
        public decimal ValorRealizado { get; set; }
    }
    public class Indicador
    {
        public List<TipoIndicador> tipoIndicador { get; set; }
    }

EDIT2

public async Task<string> PostIndicador(string jsonValue)
        {            
            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("/indicadorservice/indicadorservice.svc/metdodoservico", new StringContent("jsonValue", Encoding.UTF8, "application/json"));
                var resultcontent = await responeMessage.Content.ReadAsStringAsync();
                return (JsonConvert.DeserializeObject(resultcontent)).ToString();
            }
        }
  • Json.net allows you to serialize your objects in json, something like: string data = JsonConvert.SerializeObject(objeto); and then just pass the date variable in the function parameter. If it works, put the answer there! ;)

  • @itscorey, how do I mount the json I’ll send to the post service? In SOAP UI I step like this: {"iduser:1","idindicador:1"} and how to step into service? I did not understand your comment.

  • And if this method will be in fact post, don’t forget to add the [Frombody] and the ideal would be you build a model replicating the json object you will receive

  • @Leandroangelo, it is post yes and I am lost in how to proceed. I made a model returning the three fields that comes in the request of the service. I edited the post and put the model there.

  • @pnet, now that I have seen that your question is in sending and not in receiving

  • @Leandroangelo, I made a change in the code. I will post it in the new edition. I just need to know how I mount a json to send as parameter. I think that’s what fellow itscovery meant and I didn’t understand at the time.

Show 1 more comment
No answers

Browser other questions tagged

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