Нет данных из API в представлении переработчика с модификацией и привязкой данных

Я пытаюсь получить данные о погоде из Интернета с помощью Retrofit, и у меня есть проблема с отображением данных в программе повторного просмотра. В logcat есть все данные, поэтому API и загрузка данных работают, но утилита просмотра еще пуста.

РЕДАКТИРОВАТЬ: проблема в хранилище, где в моем хранилище, где я хочу получить данные. В logcat что-то вроде этого: ожидаемый BEGIN_ARRAY, но был BEGIN_OBJECT в строке 1, столбец 2, путь $ Это, вероятно, некоторая проблема с преобразованием JSON, но как решить эту проблему в моем случае?

Погода:

@SerializedName("coord")
@Expose
private Coord coord;
@SerializedName("weather")
@Expose
private ArrayList<Weather_> weather;
@SerializedName("base")
@Expose
private String base;
@SerializedName("main")
@Expose
private Main main;
@SerializedName("visibility")
@Expose
private int visibility;
@SerializedName("wind")
@Expose
private Wind wind;
@SerializedName("clouds")
@Expose
private Clouds clouds;
@SerializedName("dt")
@Expose
private int dt;
@SerializedName("sys")
@Expose
private Sys sys;
@SerializedName("id")
@Expose
private int id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("cod")
@Expose
private int cod;
@SerializedName("description")
@Expose
private String description;

Repository:

 public MutableLiveData<List<Weather>> getHourlyForecast(String q, String appid, String units) {

        final MutableLiveData<List<Weather>> data = new MutableLiveData<>();
        apiInterface.getHourlyForecast(q, appid, units).enqueue(new Callback<List<Weather>>() {
            @Override
            public void onResponse(Call<List<Weather>> call, Response<List<Weather>> response) {
                List<Weather> weathers = response.body();//                forecast.postValue(data.getValue());
                Log.e("HFR onResponse ", response.message());

            }

            @Override
            public void onFailure(Call<List<Weather>> call, Throwable t) {
                Log.e("HFR onFailure ", t.getMessage());
            }
        });
        return data;
    }

3 ответа

Ваш класс Weather имеет неправильную структуру, структура должна быть такой:

    -----------------------------------com.example.City.java-----------------------------------

package com.example;

import java.util.HashMap;
import java.util.Map;

public class City {

private Integer id;
private String name;
private Coord coord;
private String country;
private Integer population;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Coord getCoord() {
return coord;
}

public void setCoord(Coord coord) {
this.coord = coord;
}

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

public Integer getPopulation() {
return population;
}

public void setPopulation(Integer population) {
this.population = population;
}

public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.example.Clouds.java-----------------------------------

package com.example;

import java.util.HashMap;
import java.util.Map;

public class Clouds {

private Integer all;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

public Integer getAll() {
return all;
}

public void setAll(Integer all) {
this.all = all;
}

public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.example.Coord.java-----------------------------------

package com.example;

import java.util.HashMap;
import java.util.Map;

public class Coord {

private Double lat;
private Double lon;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

public Double getLat() {
return lat;
}

public void setLat(Double lat) {
this.lat = lat;
}

public Double getLon() {
return lon;
}

public void setLon(Double lon) {
this.lon = lon;
}

public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.example.Example.java-----------------------------------

package com.example;

import java.util.HashMap;
import java.util.Map;

public class Example {

private String cod;
private Double message;
private Integer cnt;
private java.util.List<com.example.List> list = null;
private City city;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

public String getCod() {
return cod;
}

public void setCod(String cod) {
this.cod = cod;
}

public Double getMessage() {
return message;
}

public void setMessage(Double message) {
this.message = message;
}

public Integer getCnt() {
return cnt;
}

public void setCnt(Integer cnt) {
this.cnt = cnt;
}

public java.util.List<com.example.List> getList() {
return list;
}

public void setList(java.util.List<com.example.List> list) {
this.list = list;
}

public City getCity() {
return city;
}

public void setCity(City city) {
this.city = city;
}

public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.example.List.java-----------------------------------

package com.example;

import java.util.HashMap;
import java.util.Map;

public class List {

private Integer dt;
private Main main;
private java.util.List<Weather> weather = null;
private Clouds clouds;
private Wind wind;
private Snow snow;
private Sys sys;
private String dtTxt;
private Rain rain;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

public Integer getDt() {
return dt;
}

public void setDt(Integer dt) {
this.dt = dt;
}

public Main getMain() {
return main;
}

public void setMain(Main main) {
this.main = main;
}

public java.util.List<Weather> getWeather() {
return weather;
}

public void setWeather(java.util.List<Weather> weather) {
this.weather = weather;
}

public Clouds getClouds() {
return clouds;
}

public void setClouds(Clouds clouds) {
this.clouds = clouds;
}

public Wind getWind() {
return wind;
}

public void setWind(Wind wind) {
this.wind = wind;
}

public Snow getSnow() {
return snow;
}

public void setSnow(Snow snow) {
this.snow = snow;
}

public Sys getSys() {
return sys;
}

public void setSys(Sys sys) {
this.sys = sys;
}

public String getDtTxt() {
return dtTxt;
}

public void setDtTxt(String dtTxt) {
this.dtTxt = dtTxt;
}

public Rain getRain() {
return rain;
}

public void setRain(Rain rain) {
this.rain = rain;
}

public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.example.Main.java-----------------------------------

package com.example;

import java.util.HashMap;
import java.util.Map;

public class Main {

private Double temp;
private Double tempMin;
private Double tempMax;
private Double pressure;
private Double seaLevel;
private Double grndLevel;
private Integer humidity;
private Integer tempKf;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

public Double getTemp() {
return temp;
}

public void setTemp(Double temp) {
this.temp = temp;
}

public Double getTempMin() {
return tempMin;
}

public void setTempMin(Double tempMin) {
this.tempMin = tempMin;
}

public Double getTempMax() {
return tempMax;
}

public void setTempMax(Double tempMax) {
this.tempMax = tempMax;
}

public Double getPressure() {
return pressure;
}

public void setPressure(Double pressure) {
this.pressure = pressure;
}

public Double getSeaLevel() {
return seaLevel;
}

public void setSeaLevel(Double seaLevel) {
this.seaLevel = seaLevel;
}

public Double getGrndLevel() {
return grndLevel;
}

public void setGrndLevel(Double grndLevel) {
this.grndLevel = grndLevel;
}

public Integer getHumidity() {
return humidity;
}

public void setHumidity(Integer humidity) {
this.humidity = humidity;
}

public Integer getTempKf() {
return tempKf;
}

public void setTempKf(Integer tempKf) {
this.tempKf = tempKf;
}

public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.example.Rain.java-----------------------------------

package com.example;

import java.util.HashMap;
import java.util.Map;

public class Rain {

private Double _3h;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

public Double get3h() {
return _3h;
}

public void set3h(Double _3h) {
this._3h = _3h;
}

public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.example.Snow.java-----------------------------------

package com.example;

import java.util.HashMap;
import java.util.Map;

public class Snow {

private Double _3h;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

public Double get3h() {
return _3h;
}

public void set3h(Double _3h) {
this._3h = _3h;
}

public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.example.Sys.java-----------------------------------

package com.example;

import java.util.HashMap;
import java.util.Map;

public class Sys {

private String pod;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

public String getPod() {
return pod;
}

public void setPod(String pod) {
this.pod = pod;
}

public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.example.Weather.java-----------------------------------

package com.example;

import java.util.HashMap;
import java.util.Map;

public class Weather {

private Integer id;
private String main;
private String description;
private String icon;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getMain() {
return main;
}

public void setMain(String main) {
this.main = main;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getIcon() {
return icon;
}

public void setIcon(String icon) {
this.icon = icon;
}

public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.example.Wind.java-----------------------------------

package com.example;

import java.util.HashMap;
import java.util.Map;

public class Wind {

private Double speed;
private Double deg;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

public Double getSpeed() {
return speed;
}

public void setSpeed(Double speed) {
this.speed = speed;
}

public Double getDeg() {
return deg;
}

public void setDeg(Double deg) {
this.deg = deg;
}

public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}

Вы должны установить свой адаптер, после ответа от службы.

@Override
        public void onResponse(Call<List<Weather>> call, Response<List<Weather>> response) {
            List<Weather> weathers = response.body();// 
           YourAdapter adapter = new YourAdapter(weathers);
            recyclerView.setAdapter(adapter);

        }

Я попробовал эту ссылку, когда мои данные не преобразуются в JSON автоматически во время запроса: -

https://code.i-harness.com/en/q/1b1a266

Попробуйте изучить эту ссылку

Другие вопросы по тегам