[Retrofit]:预计BEGIN_OBJECT但是BEGIN_ARRAY

时间:2015-09-01 09:18:44

标签: android json rest retrofit

此错误又回来了!

我是Retrofit的初学者,我看过这篇文章:Retrofit: Expected BEGIN_OBJECT but was BEGIN_ARRAY

但即使我尝试将其改编为我的模型,我仍然无法使其工作。所以我的错误是:

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 55 path $[0].genericDevice

我得到了什么:

[
  {
    "deviceUuid": "btB4:99:4C:59:F8:1F",
    "genericDevice": {
      "manufacturerName": "BeeWi",
      "productName": "BeeWi BBL207",
      "smartFunctions": [
        "switch",
        "rgbled",
        "whiteled"
      ]
    }
  },
  {
    "deviceUuid": "btD0:39:72:B8:93:F2",
    "genericDevice": {
      "manufacturerName": "AwoX",
      "productName": "SML-c9",
      "smartFunctions": [
        "switch",
        "whiteled",
        "rgbled"
      ]
    }
  },
  {
    "deviceUuid": "bt20:C3:8F:E4:B2:2A",
    "genericDevice": {
      "manufacturerName": "Iwedia",
      "productName": "RTRKSP",
      "smartFunctions": [
        "switch",
        "smartmeter"
      ]
    }
  },
  {
    "deviceUuid": "btA0:14:3D:0C:D3:74",
    "genericDevice": {
      "manufacturerName": "Parrot",
      "productName": "Flower power",
      "smartFunctions": [
        "switch",
        "battery",
        "plantmanager"
      ]
    }
  },
  {
    "deviceUuid": "bt78:A5:04:5A:0B:36",
    "genericDevice": {
      "manufacturerName": "BeeWi",
      "productName": "BeeWi SmartClim",
      "smartFunctions": [
        "temperaturehumidity"
      ]
    }
  },
  {
    "deviceUuid": "bt54:4A:16:59:E4:86",
    "genericDevice": {
      "manufacturerName": "AwoX",
      "productName": "AL-Bc7",
      "smartFunctions": [
        "switch",
        "rgbled",
        "whiteled",
        "oildiffuser"
      ]
    }
  }
]

解析类'属性:

private String             deviceUuid;
private IoTGenericDevice   ioTGncDvc;

IoTGenericDevice子类'attributes:

public String                   manufacturerName;
public String                   productName;
public ArrayList<String>        smartFunctions = new ArrayList<String>();

改造电话:

@GET("/iot/scanning")
public void             getIoTs(@Header(Auth.ID_SESSION_HEADER) String idSession, Callback<IoT[]> cbk);

经理:

public void             getIoTs(final CallbackIoTs cbkIoTs)
    {
        AdapterUtils.createBboxService(bbox, IBboxIoTService.class).getIoTs
                (bbox.getSessionId(), new Callback<List<IoT>>()                                       //REST call
                {
                    @Override
                    public void success(List<IoT> iots, Response rsp)
                    {
                        int statCode = rsp.getStatus();

                        if (statCode == 200) {
                            List<IoT> iotLst = Arrays.asList(iots);
                            Log.e(LOG_TAG, iotLst.get(0).getIoTGncDvc().getMnf());
                            cbkIoTs.onResult(statCode, iotLst);
                        } else {
                            Log.e(LOG_TAG, "Unexpected response while getting IoTs. HTTP code: "
                                    + String.valueOf(statCode)
                                    + " - 200 expected");
                            cbkIoTs.onResult(statCode, null);
                        }
                    }

                    @Override
                    public void failure(RetrofitError rtfErr)
                    {
                        int statCode = 500;

                        if (rtfErr.getResponse() != null)
                            statCode = rtfErr.getResponse().getStatus();
                        Log.e(LOG_TAG, "Error while getting IoTs. HTTP code: "
                                + String.valueOf(statCode)
                                + " - Server response: "
                                + rtfErr.getMessage());
                        cbkIoTs.onResult(statCode, null);
                    }
                });
    }

1 个答案:

答案 0 :(得分:1)

这应该是对象:

public class Test{

@Expose
private String deviceUuid;
@Expose
private GenericDevice genericDevice;
}

这是通用设备对象:

public class GenericDevice {

@Expose
private String manufacturerName;
@Expose
private String productName;
@Expose
private List<String> smartFunctions = new ArrayList<String>();
}

改装电话:

@GET("/iot/scanning")
public void             getIoTs(@Header(Auth.ID_SESSION_HEADER) String idSession, Callback<List<Test>> cbk);

适配器调用:

AdapterUtils.createBboxService(bbox, IBboxIoTService.class).getIoTs
                (bbox.getSessionId(), new Callback<List<Test>>(){...}