I'm building a xamarin application that connects to an API that I built and it's running fine. When I do the call to the API on xamarin I get this error:
Newtonsoft.Json.JsonSerializationException: Unable to find a constructor to use for type GesfrotaTrajetos.Models.API.Root. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'idCondutor', line 1, position 14.
I've already added the constructor and marked them with the attribute JsonConstructor, already went to the Android project properties and set the linking to Sdk and User Assemblies, and added the Newtonsoft.Json to the Skip linking Assemblies.
What am I doing wrong?
Here is my Root class:
public class Root
{
[JsonConstructor]
public Root() { }
public int idCondutor { get; set; }
public string nomeCondutor { get; set; }
public List<ListRota> listRotas { get; set; }
public RotaEmAbertoCondutor rotaEmAbertoCondutor { get; set; }
public ViaturaPreferencial viatura_preferencial { get; set; }
}
public class Viatura
{
public Viatura() { }
public int id { get; set; }
public string matricula { get; set; }
}
public class ListRota
{
public ListRota() { }
public int id { get; set; }
public string nome { get; set; }
public double distancia_ideal { get; set; }
public List<Viatura> viaturas { get; set; }
}
public class RotaEmAbertoCondutor
{
public RotaEmAbertoCondutor() { }
public int idRota { get; set; }
public object nomeRota { get; set; }
public int idViatura { get; set; }
public object matriculaViatura { get; set; }
public int idCircuito { get; set; }
public DateTime inicio { get; set; }
}
public class ViaturaPreferencial
{
public ViaturaPreferencial() { }
public int id { get; set; }
public int id_condutor { get; set; }
public int id_viatura { get; set; }
}
JsonConstructorAttributeisn't going to help one way or another since your class has an explicit public parameterless constructor yet somehow Json.NET can't find it, which suggests it has been removed by xamarin during the optimization phase. The fix suggested in the linked questions was to usePreserveAttribute; did that work?