Android Retrofit – 无法将响应添加到 List / recyclerview

huangapple 未分类评论50阅读模式
标题翻译

Android Retrofit - Unable to add response to a List / recyclerview

问题

当我获取到response.body()时,它不允许我将其设置为一个列表。

示例的GSON数据:

{
    "cod": "200",
    "message": 0,
    "cnt": 5,
    "list": [
        {
            "dt": 1590861600,
            "main": {
                "temp": 17.74,
                "feels_like": 14.52,
                "temp_min": 15.99,
                "temp_max": 17.74,
                "pressure": 1022,
                "sea_level": 1022,
                "grnd_level": 1021,
                "humidity": 51,
                "temp_kf": 1.75
            },
            "weather": [
                {
                    "id": 803,
                    "main": "Clouds",
                    "description": "broken clouds",
                    "icon": "04d"
                }
            ],
            "clouds": {
                "all": 82
            },
            "wind": {
                "speed": 3.75,
                "deg": 124
            },
            "sys": {
                "pod": "d"
            },
            "dt_txt": "2020-05-30 18:00:00"
        }
    ]
}

类:

public class WeatherResponse {

    @SerializedName("cod")
    @Expose
    private String cod;
    @SerializedName("message")
    @Expose
    private Long message;
    @SerializedName("cnt")
    @Expose
    private Long cnt;
    @SerializedName("list")
    @Expose
    private List<WeatherList> list;
    @SerializedName("city")
    @Expose
    private City city;
}

代码:

private List<WeatherResponse> mWeatherResponseList;

public void onResponse(Call<WeatherResponse> call, Response<WeatherResponse> response) {

    Log.d(TAG, "onResponse: server response " + response.toString());

    // response code 200 means a successful request
    // if successful store the response body in the log
    if (response.code() == 200) {

        mWeatherResponseList = response.body();   // 抛出错误

        // 错误提示不兼容类型: 需要 Java.util.List - 找到 WeatherResponse
    }
}

我做了以下更改:

list = new ArrayList<WeatherResponse>(Collections.singleton(((response.body()))));

这似乎已经起作用了。但现在的问题是响应中有嵌套的 JSON。它是一个对象,其中包含了一个包含5个项目的列表。当我尝试在RecyclerView中显示这5个项目时,它只显示一个对象,而不是我实际想要的5个嵌套项目的列表。

英文翻译

When I get my response.body() it will not allow me to set it to a List.

SAMPLE GSON:

 {
&quot;cod&quot;: &quot;200&quot;,
&quot;message&quot;: 0,
&quot;cnt&quot;: 5,
&quot;list&quot;: [
    {
        &quot;dt&quot;: 1590861600,
        &quot;main&quot;: {
            &quot;temp&quot;: 17.74,
            &quot;feels_like&quot;: 14.52,
            &quot;temp_min&quot;: 15.99,
            &quot;temp_max&quot;: 17.74,
            &quot;pressure&quot;: 1022,
            &quot;sea_level&quot;: 1022,
            &quot;grnd_level&quot;: 1021,
            &quot;humidity&quot;: 51,
            &quot;temp_kf&quot;: 1.75
        },
        &quot;weather&quot;: [
            {
                &quot;id&quot;: 803,
                &quot;main&quot;: &quot;Clouds&quot;,
                &quot;description&quot;: &quot;broken clouds&quot;,
                &quot;icon&quot;: &quot;04d&quot;
            }
        ],
        &quot;clouds&quot;: {
            &quot;all&quot;: 82
        },
        &quot;wind&quot;: {
            &quot;speed&quot;: 3.75,
            &quot;deg&quot;: 124
        },
        &quot;sys&quot;: {
            &quot;pod&quot;: &quot;d&quot;
        },
        &quot;dt_txt&quot;: &quot;2020-05-30 18:00:00&quot;
    },

Class

public class WeatherResponse {

@SerializedName(&quot;cod&quot;)
@Expose
private String cod;
@SerializedName(&quot;message&quot;)
@Expose
private Long message;
@SerializedName(&quot;cnt&quot;)
@Expose
private Long cnt;
@SerializedName(&quot;list&quot;)
@Expose
private List&lt;WeatherList&gt; results;
@SerializedName(&quot;city&quot;)
@Expose
private City city;

CODE

  private List&lt;WeatherResponse&gt; mWeatherResponseList;


  public void onResponse(Call&lt;WeatherResponse&gt; call, Response&lt;WeatherResponse&gt; response) {

            Log.d(TAG, &quot;onResponse: server response &quot; + response.toString());

            // response code 200 means a successful request
            // if successful store the response body in the log
            if (response.code() == 200) {

              mWeatherResponseList = response.body();   --Throws an error

The error states incompatible types: Required Java.util.list - Found WeatherResponse

I did this:

list = new ArrayList<WeatherResponse>(Collections.singleton(((response.body())))); Which appears to have worked. But the problem now is the response has nested Json. Its one object with a list of 5 items. When I try to display the 5 items in the recyclerview it only displays the one object but not the nested list of 5 items which I actually want

答案1

得分: 0

尝试一下:
像这样声明这个ArrayList

private List<WeatherList> mWeatherResponseList = new ArrayList<>();

现在在onResponse()中使用这个:

mWeatherResponseList = response.body().getResults();
英文翻译

Try this
declare this arraylist like this

private List&lt;WeatherList&gt; mWeatherResponseList=new ArrayList();

now in onResponse() use this

mWeatherResponseList = response.body().getResults();

答案2

得分: 0

你正在尝试访问整个 JSON 对象,但你只需要 JSON 对象中结果列表部分。所以在调用中你会得到单个 JSON 对象的响应,通过以下方式你可以获取结果列表。

private List<WeatherList> mWeatherResponseList;

public void onResponse(Call<WeatherResponse> call, Response<WeatherResponse> response) {

    Log.d(TAG, "onResponse: 服务器响应 " + response.toString());

    if (response.code() == 200) {

        mWeatherResponseList = response.body().results;

通过这种方式,你可以获取结果列表。

英文翻译

you are trying to access the entire JSON object but you are required with only the list of results in the JSON object.
so you are getting the single JSON object response in the call, so you could get the list of results this way.

private List&lt;WeatherList&gt; mWeatherResponseList;

public void onResponse(Call&lt;WeatherResponse&gt; call, Response&lt;WeatherResponse&gt; response) {

        Log.d(TAG, &quot;onResponse: server response &quot; + response.toString());

        if (response.code() == 200) {

          mWeatherResponseList = response.body().results;   

This way you are getting the list.

huangapple
  • 本文由 发表于 2020年5月31日 04:43:52
  • 转载请务必保留本文链接:https://java.coder-hub.com/62108455.html
匿名

发表评论

匿名网友

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

确定