Xamarin - Accessing API that returns JSON

Asked

Viewed 163 times

0

Next, I am VERY new to this API issue.
I created my first web application . net Framework 4.5.2.
I created it very standard (according to VS 2015), and created a controller for testing "Testecontroller.Cs", tested it locally and it works, I went up to a host, and it works too (http://www.afectus.com.br/api/teste/0).
Follow my API’s GET method

public string Get(int id)
    {
        ClassRetorno cr = new ClassRetorno();
        cr.Id = 1;
        cr.Nome = "BraKa Yedmore";
        cr.Email = "[email protected]";
        JavaScriptSerializer ser = new JavaScriptSerializer();
        string output = ser.Serialize(cr);
        return output;
    }

My intention is that when you touch the button, an Alert will be displayed with this information (read from the API return). Follow the method performed by tapping the button:

private async void buttonConnect_Clicked(object sender, EventArgs e)
    {
        ClassRetorno cr = await GetData();
        string retorno = "Id: " + cr.Id + ";\nNome: " + cr.Nome + ";\nE-Mail: " + cr.Email;
        await DisplayAlert("Alert", retorno, "Ok");
    }

Follows the Getdata method

public async Task<ClassRetorno> GetData()
    {
        using (var client = new HttpClient())
        {
            var response = await client.GetAsync("http://www.afectus.com.br/api/teste/0");
            if (response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync();
                //string contentTest = "{\"id\":1,\"nome\":\"BraKa Yedmore\",\"email\":\"[email protected]\"}";
                ClassRetorno cr = JsonConvert.DeserializeObject<ClassRetorno>(content);
                return cr;
            }
            else
            {
                return null;
            }
        }
    }

I did a test with the string contentTest just to see if it works. And it works!
However it does not work when populous content with API return.
This is the content of content returned from the API

string content = "\"{\\\"Id\\\":1,\\\"Nome\\\":\\\"BraKa Yedmore\\\",\\\"Email\\\":\\\"[email protected]\\\"}\"";

And this is the Exception presented in an attempt to deserialize content

Newtonsoft.Json.JsonSerializationException: Error converting value "{"Id":1,"Nome":"BraKa Yedmore","Email":"[email protected]"}" to type 'MobileApp.ClassRetorno'. Path '', line 1, position 69.

And this is the class Classretorno.Cs

public class ClassRetorno
{
    private int id;
    private string nome;
    private string email;
    public int Id { get; set; }
    public string Nome { get; set; }
    public string Email { get; set; }
}

I know it’s gotten a bit extensive in my doubt, but I tried to pass along all the information I could. If anyone knows and can help me or guide me how to solve this problem.

Thank you!

1 answer

0

I found the problem
I was trying to use the Jsonconvert.Deserializeobject(content) directly from the response I received from the API.
To solve, I had to deserialize without defining a type, and then deserialize defining the type

if (response.IsSuccessStatusCode)
            {
                var retorno = await response.Content.ReadAsStringAsync();
                object content = JsonConvert.DeserializeObject(retorno);
                ClassRetorno cr = JsonConvert.DeserializeObject<ClassRetorno>(content.ToString());
                return cr;
            }

Thank you!

Browser other questions tagged

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