英文:
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, "onResponse: " + response.body().toString());
List<Weather> list = new ArrayList<>(((Weather)response.body()));
//mWeather.postValue(response);
GSON Response
{
"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
},
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<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;
}
}
}
After that while reading your response try it this way.
ArrayList<Weather.weatherinfo> 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<Weather> list = new ArrayList<>(((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<ResponsePojo> response = getWeather(locationCode, apiKey).execute();
ResponsePojo responsePojo = response.body()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论