从 Retrofit 问题加载数据:预期 BEGIN_ARRAY 但 BEGIN_OBJECT

Loading data from Retrofit issue: Expected BEGIN_ARRAY but was BEGIN_OBJECT

首先,JSON 我从服务器获取:

{
"actor": [
    {
        "uid": "/actor-12461615214214348149",
        "subtype": "some type",
        "location": {
            "lat": 38.931,
            "lng": -77.21
        },
        "prop1": 100,
        "prop2": 1,
        "prop3": 20,
        "prop4": 80
    },
    {
        "uid": "/actor-13893903348679219933",
        "subtype": "some type",
        "location": {
            "lat": 38.93,
            "lng": -77.21
        },
        "prop1": 100,
        "prop2": 1,
        "prop3": 20,
        "prop4": 80
    },
    {
        "uid": "/actor-13809263303067864337",
        "subtype": "some type",
        "location": {
            "lat": 38.93,
            "lng": -77.21
        },
        "prop1": 100,
        "prop2": 1,
        "prop3": 20,
        "prop4": 80
    }        
  ]
}

以及我想从中加载的 POJO:

public class Actor {

private String uid;
private String subtype;
private Location loc;
private int prop1;
private int prop2;
private String prop3;
private String prop4;

public String getUid() {
    return uid;
}

public void setUid(String uid) {
    this.uid = uid;
}

public String getSubtype() {
    return subtype;
}

public void setSubtype(String subtype) {
    this.subtype = subtype;
}

public Location getLoc() {
    return loc;
}

public void setLoc(Location loc) {
    this.loc = loc;
}

public int getProp1() {
    return prop1;
}

public void setProp1(int prop1) {
    this.prop1 = prop1;
}

public int getProp2() {
    return prop2;
}

public void setProp2(int prop2) {
    this.prop2 = prop2;
}

public String getProp3() {
    return prop3;
}

public void setPower(String prop3) {
    this.prop3 = prop3;
}

public String getProp4() {
    return prop4;
}

public void setProp4(String prop4) {
    this.prop4 = prop4;
}


public class Location{
    private double lat;
    private double lng;

    public double getLng() {
        return lng;
    }
    public void setLng(double lng) {
        this.lng = lng;
    }

    public double getLat() {
        return lat;
    }
    public void setLat(double lat) {
        this.lat = lat;
    }

  }
}

我指定的 Web 服务调用正在寻找 Actor[] 响应。

在这一切之后,我看到了 RetroFit 的 IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2

我查看了此处发布的其他 RetroFit 错误,它们最常见的情况似乎是人们在获取的数据是单个对象时试图加载数组。或者,相反的情况是人们获取对象数组并尝试将其加载到单个 pojo 中。

不过,就我而言,在我看来,我尝试加载的数组与 JSON 格式的简单对象数组非常匹配。

我显然遗漏了一些东西....求另一双眼睛?

您有两种解决问题的方法:

1- 如果您有权访问服务器以更改服务,请使其提供如下响应:

[
{
    "uid": "/actor-12461615214214348149",
    "subtype": "some type",
    "location": {
        "lat": 38.931,
        "lng": -77.21
    },
    "prop1": 100,
    "prop2": 1,
    "prop3": 20,
    "prop4": 80
},
{
    "uid": "/actor-13893903348679219933",
    "subtype": "some type",
    "location": {
        "lat": 38.93,
        "lng": -77.21
    },
    "prop1": 100,
    "prop2": 1,
    "prop3": 20,
    "prop4": 80
}]

2- 如果您无法从服务器更改值,请使用参与者列表创建一个包装器 POJO 并进行改造以将其作为回调提供。

public class ActorWrapper {
    private List<Actor> actor;

    //GETTERS AND SETTERS
}