Android Retrofit – 将响应放入 ArrayList 中

huangapple go评论129阅读模式
英文:

Android Retrofit - Putting the response into an arraylist

问题

// 在后台线程上执行网络请求
try {
    Response response = getWeather(locationCode, apiKey).execute();
    if (cancelRequest) {
        return;
    }
    if (response.code() == 200) {

        //  Weather weather = response.body().getWeather();
        Log.d(TAG, "onResponse: " + response.body().toString());

        List<Weather> list = new ArrayList<>(((Weather)response.body()));
        //mWeather.postValue(response);
    }
}

GSON 响应:

{
    "coord": {
        "lon": -5.93,
        "lat": 54.58
    },
    "weather": [
        {
            "id": 804,
            "main": "Clouds",
            "description": "overcast clouds",
            "icon": "04d"
        }
    ],
    "base": "stations",
    "main": {
        "temp": 291.56,
        "feels_like": 286.36,
        "temp_min": 291.15,
        "temp_max": 292.15,
        "pressure": 1024,
        "humidity": 45
    }
}

如果有人能帮我找出问题的根本原因,我将不胜感激。

英文:

Hey guys I have retrofit successfully getting data from a rest API and its printing into the log. However when I try and capture this response into an array list I'm getting an error stating : "Cannot infer arguments (unable to resolve constructor). This error shows in the array list brackets <>

// Executes the network request on a background thread
        try {
            Response response = getWeather(locationCode, apiKey).execute();
            if (cancelRequest) {
                return;
            }
            if (response.code() == 200) {

                //  Weather weather = response.body().getWeather();
                Log.d(TAG, &quot;onResponse: &quot; + response.body().toString());

                List&lt;Weather&gt; list = new ArrayList&lt;&gt;(((Weather)response.body()));
                //mWeather.postValue(response);

GSON Response

 {
&quot;coord&quot;: {
    &quot;lon&quot;: -5.93,
    &quot;lat&quot;: 54.58
},
&quot;weather&quot;: [
    {
        &quot;id&quot;: 804,
        &quot;main&quot;: &quot;Clouds&quot;,
        &quot;description&quot;: &quot;overcast clouds&quot;,
        &quot;icon&quot;: &quot;04d&quot;
    }
],
&quot;base&quot;: &quot;stations&quot;,
&quot;main&quot;: {
    &quot;temp&quot;: 291.56,
    &quot;feels_like&quot;: 286.36,
    &quot;temp_min&quot;: 291.15,
    &quot;temp_max&quot;: 292.15,
    &quot;pressure&quot;: 1024,
    &quot;humidity&quot;: 45
},

If anyone could help me root cause this it would be greatly appreciated.

答案1

得分: 0

我觉得你创建的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;
        }
    }
}

在你的响应中这样读取

ArrayList<Weather.weatherinfo> list = response.body().getWeather();

编码愉快
英文:

I feel the POJO Class which you have created might not be proper as you have not shared your POJO class please try the below POJO class in your code an see if the error still persists.

public class Weather {
private coordinate coord;
private ArrayList&lt;weatherinfo&gt; weather;
private String base;
private maininfo main;
public coordinate getCoord() {
return coord;
}
public ArrayList&lt;weatherinfo&gt; 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;
}
}
}

After that while reading your response try it this way.

ArrayList&lt;Weather.weatherinfo&gt; list = response.body().getWeather();

Happy Coding

答案2

得分: 0

要将响应转换为可用的 Java 对象,您将需要使用 Gson 或使用 Retrofit 进行自动转换。由于您从调用中获得的是通用的 Response,这意味着您需要自己处理转换。因此,首先将 JSON 架构转换为一个 POJO,然后不再使用以下方式:

List<Weather> list = new ArrayList<>(((Weather)response.body()));

而是使用:

ResponsePojo responsePojo = gson.fromJson(response.body().toString(), ResponsePojo.class);

然而,为了不手动操作,您应该正确设置 Retrofit,以便它为您执行转换。然后,您将直接从 Retrofit 调用中获取您的对象 -

Response<ResponsePojo> response = getWeather(locationCode, apiKey).execute();
ResponsePojo responsePojo = response.body();
英文:

To convert the response to a usable java object, you will have to use Gson or use Retrofit to do it automatically. Since you are getting the generic Response from the call, it means you will have to do it yourself. So first convert the json schema to a POJO then instead of

List&lt;Weather&gt; list = new ArrayList&lt;&gt;(((Weather)response.body()));

use

ResponsePojo responsePojo = gson.fromJson(response.body().toString(), ResponsePojo.class); 

However, instead of doing it manually, you should set up Retrofit correctly so that it does the conversion for you. Then you will get back your object directly from the retrofit call -

Response&lt;ResponsePojo&gt; response = getWeather(locationCode, apiKey).execute();
ResponsePojo responsePojo = response.body()

huangapple
  • 本文由 发表于 2020年5月30日 20:38:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/62102552.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定