Api: https://api.myjson.com/bins/upt7z
Api:
public interface Api {
@GET("upt7z")
Call<List<Photo>> listData();
}
Photo:
public class Photo {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("title")
@Expose
private String title;
@SerializedName("description")
@Expose
private String description;
@SerializedName("latitude")
@Expose
private Double latitude;
@SerializedName("longitude")
@Expose
private Double longitude;
@SerializedName("url")
@Expose
private String url;
public Photo(Integer id, String title, String description, Double latitude, Double longitude, String url) {
this.id = id;
this.title = title;
this.description = description;
this.latitude = latitude;
this.longitude = longitude;
this.url = url;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Double getLatitude() {
return latitude;
}
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
Activity:
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.myjson.com/")
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
Api api = retrofit.create(Api.class);
Call<List<Photo>> photo_api = api.listData();
photo_api.enqueue(new Callback<List<Photo>>() {
@Override
public void onResponse(Call<List<Photo>> call, Response<List<Photo>> response) {
if (response.isSuccessful()) {
Log.i("if","response " + response.body().size());
} else {
Log.i("else","response code " + response.code());
}
}
@Override
public void onFailure(Call<List<Photo>> call, Throwable t) {
Log.i("onFailure",t.toString());
}
});
Ошибка:
I/onFailure: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $