Retrofit2 Data Model- Something wrong with the list

百般思念 提交于 2019-12-12 01:49:26

问题


I am using OpenWeatherMap API to retrieve forecast information for 16 days.

http://www.json-generator.com/api/json/get/cuDhZDicMO?indent=2

From a JSON like that, I am able to retrieve the value for cod. That means everything is working fine with the API. I have tested it while debugging and the API is working fine as I am able to retrieve the value of cod.

But there is something wrong with my list in the ForecastInfo data model. Could you please look at that and tell me where I've gone wrong? Because it's not retrieving the data that comes under that list. I have attached my forecastInfo which is the data model for the JSON. Thank you so much!

import java.util.ArrayList;
import java.util.List;

public class ForecastInfo {

public final double cod;

public List<LIST> list = new ArrayList<LIST>();

public List<LIST> getList() {
    return list;
}
public ForecastInfo(List<LIST> list,double cod)
{
    this.cod=cod;
    this.list=list;
}

class LIST {
    public final long dt;

    public final Temp temp;
   public LIST(long dt,Temp temp) {
        this.dt = dt;
        this.temp=temp;
    }

    class Temp
    {
        public final double day;

        public Temp(double day){
            this.day=day;

        }
    }
  }
}

回答1:


Because your model needs to have all keys that your response json file contains and name of keys should be very same. Why don't you use http://www.jsonschema2pojo.org/ site to to convert json to pojo check source type to JSON and annotation type to gson. May it can help you.Or simply get the response in String type and then convert it like
LoginResponse loginResponse = new Gson().fromJson(json, LoginResponse.class);



来源:https://stackoverflow.com/questions/43472796/retrofit2-data-model-something-wrong-with-the-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!