Stepan Stepan - 3 months ago 9
JSON Question

Expected BEGIN_ARRAY but was BEGIN_OBJECT

I am a beginner and I am using Retrofit 2 in my app. I have this JSON. (I have tried many solution but nothing worked for me). Thank for your help

Error which I have: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2

JSON

{
responseCode: 1,
responseCodeText: "ok",
response: [
{
lat: 67.2422432322,
lng: 25.1441441441,
title: "Point na mapÄ› 1",
desc: "Lorem ipsum dolor sit amet, consectetur adipisicing elit.\nVero praesentium fugiat nobis pariatur cupiditate saepe dolorum, soluta dignissimos.",
photo: [
"http://photo3.jpg",
"http://photo4.jpg",
]
},
{
lat: 39.1234787,
lng: 25.2242445,
title: "Point na mapÄ› 2",
desc: "Possimus veritatis, neque a et odit ad itaque iusto asperiores perspiciatis voluptates,\nvero praesentium fugiat nobis pariatur cupiditate saepe dolorum, soluta dignissimos.",
photo: [
"http://photo1.jpg",
"http://photo2.jpg",
]
},
//other


Interface

public interface PointsInterface {

String BASE_URL = "MY_URL/";

@POST("getPointsOnMap")
Call<List<PointsOnMap>> getPointsOnMap();

class Factory {
private static PointsInterface service;

public static PointsInterface getInstance() {
if (service == null) {
Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl(BASE_URL).build();
service = retrofit.create(PointsInterface.class);
return service;
} else {
return service;
}
}
}
}


API call

//apiService

public void serviceInit() {
PointsInterface.Factory.getInstance().getPointsOnMap().enqueue(new Callback<List<PointsOnMap>>() {
@Override
public void onResponse(Call<List<PointsOnMap>> call, Response<List<PointsOnMap>> response) {
List<PointsOnMap> result = response.body();
Log.d("response", "Number of points received: " + result.size());
}

@Override
public void onFailure(Call<List<PointsOnMap>> call, Throwable t) {
Log.e("error", "Error");
}
});}


Model

public class PointsOnMap {

@SerializedName("lat") private Double lat;
@SerializedName("lng") private Double lng;
@SerializedName("title") private String title;
@SerializedName("desc") private String desc;

public PointsOnMap(Double lat, Double lng, String title, String desc) {
this.lat = lat;
this.lng = lng;
this.title = title;
this.desc = desc;
}

public Double getLat() {
return lat;
}

public void setLat(Double lat) {
this.lat = lat;
}

public Double getLng() { return lng; }

public void setLng(Double lng) {
this.lng = lng;
}

public String getTitle() { return title; }

public void setTitle(String title) {
this.title = title;
}

public String getDesc() {
return desc;
}

public void setDesc(String desc) {
this.desc = desc;
}
}

Answer

Your Retrofit interface

     @POST("getPointsOnMap")
     Call<List<PointsOnMap>> getPointsOnMap();

Should be

     @POST("getPointsOnMap")
     Call<PointsOnMapResponse> getPointsOnMap();

where PointsOnMapResponse is

public class PointsOnMapResponse {
    @SerializedName("responseCode")
    long responseCode;

    @SerializedName("responseCodeText")
    String responseCodeText;

    @SerializedName("response")
    List<PointsOnMap> response;

    //getters, setters
}
Comments