I´m trying to get a LocalDate with Retrofit but it doesn´t works.
I get this error: E/Error: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 35 path $[0].fecha_alta Error
Retrofit class:
public class Client {
public static Retrofit getClient(String url) {
Gson gson = new GsonBuilder().registerTypeAdapter(LocalDate.class, new JsonDeserializer<LocalDate>() {
@Override
public LocalDate deserialize(JsonElement json, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
return LocalDate.parse(json.getAsString(), DateTimeFormatter.ofPattern("dd/MM/yyyy"));
}
}).create();
return new Retrofit.Builder().baseUrl(url).addConverterFactory(GsonConverterFactory.create(gson)).build();
}
} POJO:
@Generated("com.robohorse.robopojogenerator")
public class Profesores implements Serializable {
@SerializedName("dni")
private String dni;
@SerializedName("fecha_alta")
private LocalDate fecha_alta;
@SerializedName("direccion")
private String direccion;
@SerializedName("fecha_nac")
private LocalDate fecha_nac;
@SerializedName("nombre_grupo")
private String nombre_grupo;
@SerializedName("usuario")
private Usuarios usuario;
Code where it´s called:
public void getListaProfesores() {
//Se crea una instancia de llamada a la API
apiService = Network.getInstance().create(ApiService.class);
//Se llama al servicio que obtiene los profesores
Call<List<Profesores>> call = apiService.getProfesores(grupo);
call.enqueue(new Callback<List<Profesores>>() {
@Override
public void onResponse(Call<List<Profesores>> call, Response<List<Profesores>> response) {
listaProfesores = response.body();
profesorAdapter = new ProfesorAdapter(ListaProfesoresActivity.this, listaProfesores, ListaProfesoresActivity.this::onProfesorClick);
recyclerViewProfesores.setAdapter(profesorAdapter);
}
@Override
public void onFailure(Call<List<Profesores>> call, Throwable t) {
Log.e("Error", t.getMessage());
}
});
}