При инициализации API необходимо добавить преобразователь JSON.
Мой любимый Джексон:
Добавить зависимость: com.squareup.retrofit:converter-jackson
Установить преобразователь для дооснащения
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://your.domain")
.addConverterFactory(JacksonConverterFactory.create()) // In real app, you should provide a preconfigured ObjectMapper for better performance
.build();
Редактировать
Пример: * * один тысяча двадцать-одна
Создайте свою модель, соответствующую вашему JSON:
public class YourResponse {
private Detail main; // This will match "main": {}
public static final class Detail {
private String totalResults; // This will match "totalResults": ...
private String libelleCategory;
private String libelleSubCategory;
... bla bla....
... your getter/setter method....
}
... your getter/setter method....
}
Используйте его в классе API:
public interface SampleApi {
@Get("/your/path/to/get")
Call<YourResponse> getResponse();
}
Инициализируйте ваш API:
SampleApi api = new Retrofit.Builder()
.baseUrl("https://your.domain")
.addConverterFactory(JacksonConverterFactory.create()) // In real app, you should provide a preconfigured ObjectMapper for better performance
.build()
.create(SampleApi.class);
Позвоните своему API:
Response<YourResponse> serverResponse = api.getResponse().execute();
if (serverResponse.isSuccessful()) {
YourResponse = serverResponse.body();
}