Reputation: 81
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;
}
}
Upvotes: 2
Views: 2563
Reputation: 81539
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
}
Upvotes: 5
Reputation: 3561
this is because of
@POST("getPointsOnMap")
Call<List<PointsOnMap>> getPointsOnMap();
Here you remove List your error will be solved.
@POST("getPointsOnMap")
Call<PointsOnMap> getPointsOnMap();
Good luck
Upvotes: 2
Reputation: 1083
in PointsInterface replace
@POST("getPointsOnMap")
Call<List<PointsOnMap>> getPointsOnMap();
to
@POST("getPointsOnMap")
Call<PointsOnMap> getPointsOnMap();
as well as replace
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");
}
});}
to
public void serviceInit() {
PointsInterface.Factory.getInstance().getPointsOnMap().enqueue(new Callback<PointsOnMap>() {
@Override
public void onResponse(Call<PointsOnMap> call, Response<PointsOnMap> response) {
PointsOnMap result = response.body();
Log.d("response", "Number of points received: " + result.size());
}
@Override
public void onFailure(Call<PointsOnMap> call, Throwable t) {
Log.e("error", "Error");
}
});}
Upvotes: 2