我无法在 android 中使用 java 使用 retrofit2 在调用中收到响应无法解析模型中的响应
I cannot receive response in call using retrofit2 using java in android cannot parse response in model
这是一个单元素数组。我无法在 android 中使用 java 使用 retrofit2 在通话中收到它
我正在使用这个模型
@SerializedName("timelineitems")
public List<HashMap<String,Data>> data;
public class Data{
@SerializedName("new_daily_cases")
public String new_cases;
}
但在最后一行找到对象字符串
"stat": "ok"
此行调用 运行 抛出失败
"timelineitems":[
{
"4/17/20": {
"new_daily_cases": 150,
"new_daily_deaths": 16,
"total_cases": 2418,
"total_recoveries": 65,
"total_deaths": 364
},
"4/18/20": {
"new_daily_cases": 116,
"new_daily_deaths": 3,
"total_cases": 2534,
"total_recoveries": 65,
"total_deaths": 367
},
"stat": "ok"
}
]
这就是我用来解决问题的模型
public class 型号{
@SerializedName("timelineitems")
public List<HashMap<String,Object>> data;
public class Data{
@SerializedName("new_daily_cases")
public String new_cases;
@SerializedName("new_daily_deaths")
public String new_daily_deaths;
@SerializedName("total_cases")
public String total_cases;
@SerializedName("total_recoveries")
public String total_recoveries;
@SerializedName("total_deaths")
public String total_deaths;
}
}
然后我使用此代码获取我的数据
尝试{ 模型资源=response.body();
HashMap<String,Object>data=resource.data.get(0);
Gson g = new Gson();
Data inf = g.fromJson(g.toJson(resource.data.get(0).get(date0)), Data.class);
}catch (NullPointerException e){
=
Toast.makeText(getApplicationContext(),"No data available",Toast.LENGTH_LONG).show();
}
这是一个单元素数组。我无法在 android 中使用 java 使用 retrofit2 在通话中收到它 我正在使用这个模型
@SerializedName("timelineitems")
public List<HashMap<String,Data>> data;
public class Data{
@SerializedName("new_daily_cases")
public String new_cases;
}
但在最后一行找到对象字符串
"stat": "ok"
此行调用 运行 抛出失败
"timelineitems":[
{
"4/17/20": {
"new_daily_cases": 150,
"new_daily_deaths": 16,
"total_cases": 2418,
"total_recoveries": 65,
"total_deaths": 364
},
"4/18/20": {
"new_daily_cases": 116,
"new_daily_deaths": 3,
"total_cases": 2534,
"total_recoveries": 65,
"total_deaths": 367
},
"stat": "ok"
}
]
这就是我用来解决问题的模型 public class 型号{
@SerializedName("timelineitems")
public List<HashMap<String,Object>> data;
public class Data{
@SerializedName("new_daily_cases")
public String new_cases;
@SerializedName("new_daily_deaths")
public String new_daily_deaths;
@SerializedName("total_cases")
public String total_cases;
@SerializedName("total_recoveries")
public String total_recoveries;
@SerializedName("total_deaths")
public String total_deaths;
}
}
然后我使用此代码获取我的数据
尝试{ 模型资源=response.body();
HashMap<String,Object>data=resource.data.get(0);
Gson g = new Gson();
Data inf = g.fromJson(g.toJson(resource.data.get(0).get(date0)), Data.class);
}catch (NullPointerException e){
=
Toast.makeText(getApplicationContext(),"No data available",Toast.LENGTH_LONG).show();
}