How do i consume a REST service in xamarin?

Jonathan0931Jonathan0931 Member ✭✭
edited July 2018 in General

Hi, and sorry for my bad english,

I have a problem, i try to consume a Rest service from a PCL class, My code is

                    HttpClient client = new HttpClient();
                    ResultService<string> Items = new ResultService<string>();
                    var URL = new Uri(string.Format(uri, string.Empty));
                    var json = JsonConvert.SerializeObject(parametros);

                    var content = new StringContent(json, Encoding.UTF8, "application/json");

                    var response = client.PostAsync(uri, content).GetAwaiter().GetResult();

                   var result = await response.Content.ReadAsStringAsync();
                   Items = JsonConvert.DeserializeObject<ResultService<string>>(result);

                  return Items;
                catch (Exception ex)
                    ResultService<string> x = new ResultService<string>();
                    x.Object = "ERROR:";
                    return x;

the REST service its ok i check with

and its ok, In the code throw a Exception in the catch i have "A task was cancelled"

~~~~i change this line

var response = client.PostAsync(uri, content).GetAwaiter().GetResult();

for this

var response = client.PostAsync(uri, content).Result;

and a lot of things more.

i been trying for days

Please help and thanks

Sign In or Register to comment.