Подскажите в чем ошибка, почему не могу извлечь данные?
Использую библиотеки retrofit. У меня структура JSON вот такая:
"routes":[
{
"platforms":[
{
"estimated_fill":1.0,
"volume":1.100,
"address":"АТБ-Містечко",
"lat":50.2107736547029,
"lng":30.3174322843552,
"id":1022549,
"quantity":1
},
{
"estimated_fill":1.0,
"volume":1.100,
"address":"пр.Лесі Українки, 15 ",
"lat":50.1974241245373,
"lng":30.2944350242615,
"id":1022542,
"quantity":1
},
{
"estimated_fill":1.0,
"volume":1.100,
"address":"Столичка",
"lat":50.216073975587,
"lng":30.3178024291992,
"id":1022543,
"quantity":3
},
{
"estimated_fill":1.0,
"volume":1.100,
"address":"Солнечная",
"lat":50.1883300230983,
"lng":30.2941560745239,
"id":1022544,
"quantity":1
},
{
"estimated_fill":1.0,
"volume":1.100,
"address":"вул.Володимирська,13",
"lat":50.1747131083508,
"lng":30.3193259239197,
"id":1022545,
"quantity":1
},
{
"estimated_fill":1.0,
"volume":1.100,
"address":"вул.Володимирька, 28 (Корунд)",
"lat":50.1733834992561,
"lng":30.3227108716965,
"id":1022546,
"quantity":1
},
{
"estimated_fill":1.0,
"volume":1.100,
"address":"Фуршет",
"lat":50.1795055782639,
"lng":30.3133499622345,
"id":1022547,
"quantity":1
},
{
"estimated_fill":1.0,
"volume":1.100,
"address":"вул.Набережна",
"lat":50.1800174337311,
"lng":30.3084254264832,
"id":1022548,
"quantity":1
},
{
"estimated_fill":1.0,
"volume":1.100,
"address":"Військове .Містечко",
"lat":50.2120369970609,
"lng":30.3167670965195,
"id":1022550,
"quantity":1
},
{
"estimated_fill":1.0,
"volume":1.100,
"address":"вул.Грушевського,25",
"lat":50.180299123662,
"lng":30.3108340501785,
"id":1022551,
"quantity":1
},
{
"estimated_fill":1.0,
"volume":1.100,
"address":"Іванковичі",
"lat":50.2786446089967,
"lng":30.4251337051392,
"id":1022552,
"quantity":2
}
],
"num":"01.05 - Смаколики",
"id":1022554,
"itinerary":""
}
],
"success":true
Вот мой интерфес :
@Headers({("X-Signature: reserved"),("Content-Type: text/x-json")})
@POST("get_routes")
Call <MyRoutes> getRoutes(@Body Map<String, String> id);
Вот мой retrofit:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://gps.alematics.com/") // Адрес сервера/
.addConverterFactory(GsonConverterFactory.create()) // говорим ретрофиту что для сериализации необходимо использовать GSON
.build();
jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
Вот так звоню:
public void getRouteList() {
Map<String, String> parameters = new HashMap<>();
parameters.put("device_id", "1789fa18ad90ba55");
Call<MyRoutes> call = jsonPlaceHolderApi.getRoutes(parameters);
call.enqueue(new Callback<MyRoutes> () {
@SuppressLint("SetTextI18n")
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onResponse(Call<MyRoutes> call, Response<MyRoutes> response) {
List<MyRoutes>rout=Collections.singletonList(response.body());
if (!response.isSuccessful()) {
Log.d("asd", "no" + response.errorBody());
return;
} else {
EditText editText = findViewById(R.id.editText4);
Log.d("asd", "" + myRoutes.getRoutes());
}
}
}
@Override
public void onFailure(Call<MyRoutes> call, Throwable t) {
Log.d("asd", "error");
}
});
MyRoutes:
@SerializedName("routes")
@Expose
private List routes = null;
@SerializedName("success")
@Expose
private Boolean success;
public List getRoutes() {
return routes;
}
public void setRoutes(List routes) {
this.routes = routes;
}
public Boolean getSuccess() {
return success;
}
public void setSuccess(Boolean success) {
this.success = success;
}
Routes:
@Generated("jsonschema2pojo")
public class Route {
@SerializedName("platforms")
@Expose
private List<Platform> platforms = null;
@SerializedName("num")
@Expose
private String num;
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("itinerary")
@Expose
private String itinerary;
public List<Platform> getPlatforms() {
return platforms;
}
public void setPlatforms(List<Platform> platforms) {
this.platforms = platforms;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getItinerary() {
return itinerary;
}
public void setItinerary(String itinerary) {
this.itinerary = itinerary;
}
Platforms:
@Generated("jsonschema2pojo")
public class Platform {
@SerializedName("estimated_fill")
@Expose
private Float estimatedFill;
@SerializedName("volume")
@Expose
private Float volume;
@SerializedName("address")
@Expose
private String address;
@SerializedName("lat")
@Expose
private Float lat;
@SerializedName("lng")
@Expose
private Float lng;
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("quantity")
@Expose
private Integer quantity;
public Float getEstimatedFill() {
return estimatedFill;
}
public void setEstimatedFill(Float estimatedFill) {
this.estimatedFill = estimatedFill;
}
public Float getVolume() {
return volume;
}
public void setVolume(Float volume) {
this.volume = volume;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Float getLat() {
return lat;
}
public void setLat(Float lat) {
this.lat = lat;
}
public Float getLng() {
return lng;
}
public void setLng(Float lng) {
this.lng = lng;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getQuantity() {
return quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
Ответы (1 шт):
Автор решения: Андрей Мостовенко
→ Ссылка
Вот что у меня получилось и решило мою проблему:
List<Route> trt = response.body().getRoutes();
for(Route rtr : trt){
List<Platform> pl = rtr.getPlatforms();
for(Platform plt : pl){
Log.d("asd", "" + plt.getLng());
String lon = String.valueOf(plt.getLng());
editText.setText("" + lon);
}