Java Android改型-将响应放入arraylist
嘿,伙计们,我已经改装成功地从RESTAPI获取数据并将其打印到日志中。但是,当我尝试将此响应捕获到数组列表中时,我会收到一个错误声明:“无法推断参数(无法解析构造函数)。此错误显示在数组列表括号中Java Android改型-将响应放入arraylist,java,android,retrofit2,android-mvvm,Java,Android,Retrofit2,Android Mvvm,嘿,伙计们,我已经改装成功地从RESTAPI获取数据并将其打印到日志中。但是,当我尝试将此响应捕获到数组列表中时,我会收到一个错误声明:“无法推断参数(无法解析构造函数)。此错误显示在数组列表括号中 如果有人能帮我解决这个问题,我们将不胜感激。我觉得您创建的POJO类可能不合适,因为您没有共享您的POJO类。请在代码中尝试下面的POJO类,看看错误是否仍然存在 public class Weather { private coordinate coord; private ArrayList&
如果有人能帮我解决这个问题,我们将不胜感激。我觉得您创建的POJO类可能不合适,因为您没有共享您的POJO类。请在代码中尝试下面的POJO类,看看错误是否仍然存在
public class Weather {
private coordinate coord;
private ArrayList<weatherinfo> weather;
private String base;
private maininfo main;
public coordinate getCoord() {
return coord;
}
public ArrayList<weatherinfo> getWeather() {
return weather;
}
public String getBase() {
return base;
}
public maininfo getMain() {
return main;
}
public static class coordinate{
String lon,lat;
public String getLon() {
return lon;
}
public String getLat() {
return lat;
}
}
public static class weatherinfo{
String id,main,description,icon;
public String getId() {
return id;
}
public String getMain() {
return main;
}
public String getDescription() {
return description;
}
public String getIcon() {
return icon;
}
}
public static class maininfo{
String temp,feels_like,temp_min,temp_max,pressure,humidity;
public String getTemp() {
return temp;
}
public String getFeels_like() {
return feels_like;
}
public String getTemp_min() {
return temp_min;
}
public String getTemp_max() {
return temp_max;
}
public String getPressure() {
return pressure;
}
public String getHumidity() {
return humidity;
}
}
}
公共类天气{
私人协调机构;
私人夜总会天气;
私有字符串基;
私有主信息;
公共坐标getCoord(){
返回坐标;
}
公共阵列列表getWeather(){
回归天气;
}
公共字符串getBase(){
返回基地;
}
公共mainfo getMain(){
回水总管;
}
公共静态类坐标{
弦长,纬度;
公共字符串getLon(){
返回离子;
}
公共字符串getLat(){
返回lat;
}
}
公共静态类weatherinfo{
字符串id、主、描述、图标;
公共字符串getId(){
返回id;
}
公共字符串getMain(){
回水总管;
}
公共字符串getDescription(){
返回说明;
}
公共字符串getIcon(){
返回图标;
}
}
公共静态类mainfo{
串温、手感、最低温度、最高温度、压力、湿度;
公共字符串getTemp(){
返回温度;
}
公共字符串getFeels_like(){
回归的感觉;
}
公共字符串getTemp_min(){
返回温度最低值;
}
公共字符串getTemp_max(){
返回温度_max;
}
公共字符串getPressure(){
回流压力;
}
公共字符串getweathy(){
返回湿度;
}
}
}
之后,在阅读你的回答时,试着这样做
ArrayList<Weather.weatherinfo> list = response.body().getWeather();
ArrayList list=response.body().getWeather();
快乐编码我觉得您创建的POJO类可能不合适,因为您没有共享您的POJO类。请在代码中尝试下面的POJO类,看看错误是否仍然存在
public class Weather {
private coordinate coord;
private ArrayList<weatherinfo> weather;
private String base;
private maininfo main;
public coordinate getCoord() {
return coord;
}
public ArrayList<weatherinfo> getWeather() {
return weather;
}
public String getBase() {
return base;
}
public maininfo getMain() {
return main;
}
public static class coordinate{
String lon,lat;
public String getLon() {
return lon;
}
public String getLat() {
return lat;
}
}
public static class weatherinfo{
String id,main,description,icon;
public String getId() {
return id;
}
public String getMain() {
return main;
}
public String getDescription() {
return description;
}
public String getIcon() {
return icon;
}
}
public static class maininfo{
String temp,feels_like,temp_min,temp_max,pressure,humidity;
public String getTemp() {
return temp;
}
public String getFeels_like() {
return feels_like;
}
public String getTemp_min() {
return temp_min;
}
public String getTemp_max() {
return temp_max;
}
public String getPressure() {
return pressure;
}
public String getHumidity() {
return humidity;
}
}
}
公共类天气{
私人协调机构;
私人夜总会天气;
私有字符串基;
私有主信息;
公共坐标getCoord(){
返回坐标;
}
公共阵列列表getWeather(){
回归天气;
}
公共字符串getBase(){
返回基地;
}
公共mainfo getMain(){
回水总管;
}
公共静态类坐标{
弦长,纬度;
公共字符串getLon(){
返回离子;
}
公共字符串getLat(){
返回lat;
}
}
公共静态类weatherinfo{
字符串id、主、描述、图标;
公共字符串getId(){
返回id;
}
公共字符串getMain(){
回水总管;
}
公共字符串getDescription(){
返回说明;
}
公共字符串getIcon(){
返回图标;
}
}
公共静态类mainfo{
串温、手感、最低温度、最高温度、压力、湿度;
公共字符串getTemp(){
返回温度;
}
公共字符串getFeels_like(){
回归的感觉;
}
公共字符串getTemp_min(){
返回温度最低值;
}
公共字符串getTemp_max(){
返回温度_max;
}
公共字符串getPressure(){
回流压力;
}
公共字符串getweathy(){
返回湿度;
}
}
}
之后,在阅读你的回答时,试着这样做
ArrayList<Weather.weatherinfo> list = response.body().getWeather();
ArrayList list=response.body().getWeather();
快乐编码要将响应转换为可用的java对象,您必须使用Gson或改型来自动完成。由于您从调用中获得了通用的
响应
,这意味着您必须自己完成。因此,首先将json模式转换为POJO,然后将其转换为
List<Weather> list = new ArrayList<>(((Weather)response.body()));
但是,与手动操作不同,您应该正确设置改装,以便它为您进行转换。然后您将直接从改装调用中返回您的对象-
Response<ResponsePojo> response = getWeather(locationCode, apiKey).execute();
ResponsePojo responsePojo = response.body()
Response-Response=getWeather(locationCode,apiKey).execute();
ResponsePojo ResponsePojo=response.body()
要将响应转换为可用的java对象,您必须使用Gson或改型来自动完成。由于您从调用中获得了通用的响应,这意味着您必须自己完成。因此,首先将json模式转换为POJO,然后再将其转换为
List<Weather> list = new ArrayList<>(((Weather)response.body()));
但是,与手动操作不同,您应该正确设置改装,以便它为您进行转换。然后您将直接从改装调用中返回您的对象-
Response<ResponsePojo> response = getWeather(locationCode, apiKey).execute();
ResponsePojo responsePojo = response.body()
Response-Response=getWeather(locationCode,apiKey).execute();
ResponsePojo ResponsePojo=response.body()
您是否添加了具有如下改装功能的gson转换器?addConverterFactory(GsonConverterFactory.create())是的,gson解析很好,我可以在日志中看到它。它将其存储在数组列表中,这给我带来了麻烦您是否添加了具有如下改装功能的gson转换器?addConverterFactory(GsonConverterFactory.create())是的,gson解析很好,我可以在日志中看到它。它将它存储在arraylist中,这给我带来了麻烦谢谢你的回答,但是pojo类可以工作,并将数据返回到log np中。不幸的是,你推荐的arraylist语法不起作用。但是谢谢你的帮助,我很感激。谢谢你的回答,但是pojo类工作并将数据返回到日志np中。不幸的是,您推荐的ArrayList语法不起作用。但是感谢您的帮助,我非常感谢。