Retrofit2数据模型-列表出了点问题 [英] Retrofit2 Data Model- Something wrong with the list

查看:125
本文介绍了Retrofit2数据模型-列表出了点问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用OpenWeatherMap API来检索16天的天气预报信息.

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

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

从这样的JSON中,我可以检索cod的值.这意味着该API一切正常.我已经在调试时进行了测试,并且API可以正常工作,因为我可以检索cod的值.

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.

但是我在ForecastInfo数据模型中的列表有问题.您能看一下,然后告诉我我哪里出问题了吗?因为它没有检索该列表下的数据.我已经附加了forecastInfo,它是JSON的数据模型.非常感谢!

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;

        }
    }
  }
}

推荐答案

因为您的模型需要具有响应json文件包含的所有键,并且键的名称应该非常相同.为什么不使用 http://www.jsonschema2pojo.org/网站将json转换为pojo将源类型检查为JSON,将注释类型检查为gson.可能对您有帮助.或者只是以String类型获取响应,然后将其转换为
LoginResponse loginResponse = new Gson().fromJson(json,LoginResponse.class);

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);

这篇关于Retrofit2数据模型-列表出了点问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆