'E/fail: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path
I started learn retrofit library and I have one problem
My API is https://alien-backend.herokuapp.com/api/tutorials
public interface UserService {
@GET("api/tutorials?type=json")
Call<List<UserResponse>> getAllUsers();
}
public class UserResponse {
private String id;
private String title;
private String description;
private String image;
private String createdAt;
private String updatedAt;
public String getId() {
return id;
}
public void setId(String 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 String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getCreatedAt() {
return createdAt;
}
public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}
public String getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(String updatedAt) {
this.updatedAt = updatedAt;
}
@Override
public String toString() {
return "UserResponse{" +
"id='" + id + '\'' +
", title='" + title + '\'' +
", description='" + description + '\'' +
", image='" + image + '\'' +
", createdAt='" + createdAt + '\'' +
", updatedAt='" + updatedAt + '\'' +
'}';
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Call<List<UserResponse>> userlist = ApiClient.getUserService().getAllUsers();
userlist.enqueue(new Callback<List<UserResponse>>() {
@Override
public void onResponse(Call<List<UserResponse>> call, Response<List<UserResponse>> response) {
if (response.isSuccessful()){
Log.e("success", response.body().toString());
}
}
@Override
public void onFailure(Call<List<UserResponse>> call, Throwable t) {
Log.e("fail", t.getLocalizedMessage());
}
});
}
}
public class ApiClient { private static Retrofit getRetrofit(){
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor).build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://alien-backend.herokuapp.com/")
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
return retrofit;
}
public static UserService getUserService(){
UserService userService = getRetrofit().create(UserService.class);
return userService;
}
}
When I try to run app I get a problem
D/OkHttp: {"totalItems":3,"tutorials":[{"title":"Dog 4","description":"This is my dog , maybe","image":"https://faunistics.com/wp-content/uploads/2019/06/3-9.jpg","createdAt":"2022-03-09T16:32:35.318Z","updatedAt":"2022-03-10T10:43:22.926Z","id":"6228d6a38989627eb8d404d2"},{"title":"Dog 2","description":"Какое-то описанеи для второго поста","image":"https://pbs.twimg.com/media/CyIKGO4WgAA0K5K.jpg:large","createdAt":"2022-03-09T17:55:14.377Z","updatedAt":"2022-03-09T17:55:14.377Z","id":"6228ea02226449d633e51545"},{"title":"Dog 3","description":"А это уже третий пост","image":"https://klkfavorit.ru/wp-content/uploads/2/f/a/2fa86dbececb4b25587ccf2bc4da84cd.jpeg","createdAt":"2022-03-09T17:55:48.463Z","updatedAt":"2022-03-09T17:55:48.463Z","id":"6228ea24226449d633e51547"}],"totalPages":1,"currentPage":0} D/OkHttp: <-- END HTTP (848-byte body) E/fail: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ D/InputMethodManager: prepareNavigationBarInfo() DecorView@8e0fe86[MainActivity]
Can anybody help me ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
