【问题标题】:Retrofit: Expected BEGIN_OBJECT but was BEGIN_ARRAY改造:预期 BEGIN_OBJECT 但为 BEGIN_ARRAY
【发布时间】:2015-07-24 10:56:48
【问题描述】:

很抱歉再问一个关于此错误的问题,但我所阅读的所有内容对我来说都是无助的。

我正在使用 Retrofit 库和 GSON 来解析 JSON 答案。我收到了这个错误:

E/RETROFIT ERROR﹕ com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

JSON 答案:

[
  {
"route_id": 16,
"typeofmovement_name": "Type name",
"route_description": "Just default description and no more",
"route_name": "Default name",
"route_img_url": "place reserved for url",
"themes": [
  "Theme name one",
  "Theme name two"
],
"routeparts": [
  {
    "rp_id": 32,
    "part_id": 1,
    "rp_start_latitude": "59.935353200891576",
    "rp_start_longitude": "30.31521778553724",
    "rp_end_latitude": "59.93948523234933",
    "rp_end_longitude": "30.314136184751987",
    "rp_description": "Default Description",
    "rp_title": "Default Title"
  },
  {
    "rp_id": 33,
    "part_id": 2,
    "rp_start_latitude": "59.93948523234933",
    "rp_start_longitude": "30.314136184751987",
    "rp_end_latitude": "59.940330654891774",
    "rp_end_longitude": "30.328612737357613",
    "rp_description": "Default Description",
    "rp_title": "Default Title"
  },
  {
    "rp_id": 34,
    "part_id": 3,
    "rp_start_latitude": "59.940330654891774",
    "rp_start_longitude": "30.328612737357613",
    "rp_end_latitude": "59.934541749310746",
    "rp_end_longitude": "30.33052649348974",
    "rp_description": "Default Description",
    "rp_title": "Default Title"
  }
]
 }
]

类,我用于 GSON 解析

public class Route {
@SerializedName("route_id") private int routeId;
@SerializedName("typeofmovement_name") private String typeOfMove;
@SerializedName("route_description") private String routeDescription;
@SerializedName("route_name") private String routeTitle;
@SerializedName("route_img_url") private String imgUrl;
@SerializedName("themes") private String[] themes;
private List<Routeparts> routeparts;
Route() {
    this.routeparts = new ArrayList<>();
}

class Routeparts {
    int rp_id;
    int part_id;
    String rp_start_latitude;       
    String rp_start_longitude;
    String rp_end_latitude;
    String rp_end_longitude;
    String rp_description;
    String rp_title;
}

有 Retrofit API

@GET("/routes/{route_id}")
void getRouteInfo(@Path("route_id") String routeId, Callback<Route> callback);

附:对可能的语法错误深表歉意。

【问题讨论】:

  • 请出示您用于阅读的句子。您似乎正在尝试读取单个对象,而 JSON 消息是一个列表。

标签: android json gson retrofit


【解决方案1】:

你的改造服务应该是这样的

@GET("/Routes") void getRoutes(Callback<List<Route>> routesCallback);

你的电话应该是这样的

RetrofitService.getRoutes(new Callback<List<Route>>() {
    @Override public void success(List<Route> routes, Response response) {
       //success        
    }

    @Override public void failure(RetrofitError error) {
        //error
    }
});

如果您真的希望使用单一路由,则必须更改服务器代码。

【讨论】:

    猜你喜欢
    • 2014-08-01
    • 2015-11-26
    • 2018-03-27
    • 2016-06-28
    • 2019-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-04
    相关资源
    最近更新 更多