代码之家  ›  专栏  ›  技术社区  ›  Konstantin

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

  •  0
  • Konstantin  · 技术社区  · 9 年前

    请告知。我有一个复杂的json对象,我请求使用Reformation和GSONConverterFactory打开WeatherMap API。我在请求5天的预测时遇到了问题,我无法用数据填充我的Recyclerview,出现了一些问题。我无法在改装回调的onResponse方法中得到应该写的内容。

    提前谢谢!

    下面是Json对象的结构

    Structure of Json object

    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。适配器

    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 回复  |  直到 9 年前
        1
  •  0
  •   Ayush Khare    9 年前

    你的 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 所以你的

          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();
                List<Weather> weatherList = data.getWeatherList(); 
               //Pass this list to your adapter
           }
    
        }
    
        @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();
        }
    });
    
        2
  •  0
  •   Philipp Zitzmann    9 年前

    JSON返回是一个列表,而不仅仅是一个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();
    }