如何使用改造使用compex json数据填充recyclerview?

时间:2017-07-01 10:41:24

标签: android json android-recyclerview retrofit2 openweathermap

请指教。我有一个复杂的json对象,我使用Retrofit和GSONConverterFactory请求openweathermap API。我在请求预测5天时遇到问题,我无法使用数据填充我的Recyclerview,出现问题。我无法在reRofit回调的onResponse方法中写出什么。

改造请求全部使用代码200并且消息OK。所以问题不在这个领域。

提前谢谢!

这是Json对象的结构

Structure of Json object

WeatherData是一个根对象,我可以解析我的Json,请找到下面的代码。它的所有代码(以及其他POJO'都是从jsonschema2pojo导入的:

public class WeatherData {

@SerializedName("coord")
@Expose
private Coord coord;
@SerializedName("weather")
@Expose
private List<Weather> weather = null;
@SerializedName("base")
@Expose
private String base;
@SerializedName("main")
@Expose
private Main main;
@SerializedName("visibility")
@Expose
private Integer visibility;
@SerializedName("wind")
@Expose
private Wind wind;
@SerializedName("clouds")
@Expose
private Clouds clouds;
@SerializedName("dt")
@Expose
private Long dt;
@SerializedName("sys")
@Expose
private Sys sys;
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("cod")
@Expose
private Integer cod;

public Coord getCoord() {
    return coord;
}

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

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

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

public String getBase() {
    return base;
}

public void setBase(String base) {
    this.base = base;
}

public Main getMain() {
    return main;
}

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

public Integer getVisibility() {
    return visibility;
}

public void setVisibility(Integer visibility) {
    this.visibility = visibility;
}

public Wind getWind() {
    return wind;
}

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

public Clouds getClouds() {
    return clouds;
}

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

public Long getDt() {
    return dt;
}

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

public Sys getSys() {
    return sys;
}

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

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 Integer getCod() {
    return cod;
}

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

这是我的RecyclerView.Adapter

public class Forecast5DaysAdapter extends RecyclerView.Adapter<Forecast5DaysAdapter.ForecastHolder> {



List<WeatherData> mWeatherDataList;


public static class ForecastHolder extends RecyclerView.ViewHolder {

    public TextView dateOnDate;
    public ImageView weatherOnDate;
    public TextView tempOnDate;
    public TextView windSpeedOnDate;

    public ForecastHolder(View view) {
        super(view);

        dateOnDate = (TextView) view.findViewById(R.id.dateOnDate);
        windSpeedOnDate = (TextView) view.findViewById(R.id.windSpeedOnDate);
        tempOnDate = (TextView) view.findViewById(R.id.tempOnDate);
        weatherOnDate = (ImageView) view.findViewById(R.id.imageOnDate);


    }
}

public Forecast5DaysAdapter(List<WeatherData> mWeatherDataList) {

    this.mWeatherDataList = mWeatherDataList;
}

@Override
public ForecastHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.forecast_card, parent, false);

    final ForecastHolder forecastHolder = new ForecastHolder(view);

    return forecastHolder;
}

@Override
public void onBindViewHolder(ForecastHolder holder, int position) {

    //FILLING THE CARDS IN RECYCLERVIEW WITH INFORMATION

    holder.dateOnDate.setText(mWeatherDataList.get(position).getDt().toString());
    holder.tempOnDate.setText(mWeatherDataList.get(position).getMain().getTemp().toString());
    holder.windSpeedOnDate.setText(mWeatherDataList.get(position).getWind().getSpeed().toString());
    Picasso.with(holder.weatherOnDate.getContext()).load("http://openweathermap.org/img/w/" + mWeatherDataList.get(position).getWeather().get(position).getIcon() + ".png").into(holder.weatherOnDate);

}


@Override
public int getItemCount() {
    return 0;
}

这是我想要显示Recyclerview的课程

public class Forecast5Days extends AppCompatActivity {

private static final String API_KEY = "HERE IS THE KEY";

private RecyclerView forecastRecycler;
private ArrayList<WeatherData> mWeatherData;
private Forecast5DaysAdapter forecast5DaysAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_forecast_5_days);

    forecastRecycler = (RecyclerView) findViewById(R.id.forecast_5_daysRecycler);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
    forecastRecycler.setLayoutManager(layoutManager);
    final Forecast5DaysAdapter forecast5DaysAdapter = new Forecast5DaysAdapter(mWeatherData);

    forecastRecycler.setAdapter(forecast5DaysAdapter);



    Intent intent = getIntent();
    final String cityName = intent.getStringExtra("cityName");

    if (!cityName.isEmpty()) {
        Call<WeatherData> call = RetrofitBuilderHelper.weatherAPI.getForecast5Days(cityName, "ru", "metric", API_KEY);

        call.enqueue(new Callback<WeatherData>() {
            @Override
            public void onResponse(Call<WeatherData> call, Response<WeatherData> response) {

                    //????????????????

            }

            @Override
            public void onFailure(Call<WeatherData> call, Throwable t) {
                Toast toast = Toast.makeText(Forecast5Days.this, "Something went wrong with request", Toast.LENGTH_LONG);
                toast.show();
            }
        });

    } else {
        Toast toast = Toast.makeText(Forecast5Days.this, "Something went wrong with intent", Toast.LENGTH_LONG);
        toast.show();

    }


}

2 个答案:

答案 0 :(得分:0)

您的JSON返回不仅是一个List WeatherData对象 所以你应该做的就是预期回报价值 试试这个:

Call<List<WeatherData>> call = RetrofitBuilderHelper.weatherAPI.getForecast5Days(cityName, "ru", "metric", API_KEY);

        call.enqueue(new Callback<List<WeatherData>>() {
@Override
public void onResponse(Call<List<WeatherData>> call, Response<List<WeatherData>> response) {
    forecastRecycler.weatherList = response.body();
    forecastRecycler.notifyDatasetChanged();
}

答案 1 :(得分:0)

您的onActivityCreated应该是这样的

onResponse

另外,您的 call.enqueue(new Callback<WeatherData>() { @Override public void onResponse(Call<WeatherData> call, Response<WeatherData> response) { if(response.isSuccessful() && response.body != null) { WeatherData data = response.body(); } } @Override public void onFailure(Call<WeatherData> call, Throwable t) { Toast toast = Toast.makeText(Forecast5Days.this, "Something went wrong with request", Toast.LENGTH_LONG); toast.show(); } }); Call,它会为您提供一个对象。如果您想要一个对象列表,您的呼叫应该是Call<WeatherData>

我认为您希望通过Call<List<WeatherData>>代替Weather,以便WeatherData看起来像

onResponse