Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.8k views
in Technique[技术] by (71.8m points)

android - Retrofit: Expected BEGIN_OBJECT but was BEGIN_ARRAY

Sorry for one more question about this error, but everything that I've read was helpless for me.

I'm using Retrofit Library and GSON for parse JSON answers. I got this error:

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

JSON answer:

[
  {
"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"
  }
]
 }
]

Class, that I use for GSON parse

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;
}

There are Retrofit API

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

P.S. Sorry for possible grammar mistakes.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You Retrofit service should be something like

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

And your call should be something like

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

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

If you are really expecting a single route, you will have to change the server code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...