I have been trying to use Stack Exchange API in my Android App.
This is my code where I call the API:
public interface StackExchange {
@GET("/2.3/posts?page=1&pagesize=1&order=desc&sort=activity&site=stackoverflow")
Call<List<Articles>> getArticles();
}
And this is where I display my result:
private Retrofit getRetrofit() {
return new Retrofit.Builder()
.baseUrl("https://api.stackexchange.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
}
private StackExchange getApi(){
return getRetrofit().create(StackExchange.class);
}
private void getRetrofitData(){
getApi().getArticles().enqueue(new Callback<List<Articles>>() {
@Override
public void onResponse(Call<List<Articles>> call, Response<List<Articles>> response) {
if(!response.isSuccessful()){
textViewResult.setText(("Code: " + response.code()));
}
List<Articles> articlesList = response.body();
for (Articles articles : articlesList){
String content = "";
content += "User type: " + articles.getUser_type() + "\n";
content += "Display name: " + articles.getDisplay_name() + "\n";
content += "Link: " + articles.getLink() + "\n\n";
textViewResult.append(content);
}
}
@Override
public void onFailure(Call<List<Articles>> call, Throwable t) {
textViewResult.setText(t.getMessage());
}
});
}
The response of the api is this:
{"items":[{"owner":{"account_id":5437531,"reputation":338,"user_id":4326551,"user_type":"registered","profile_image":"https://www.gravatar.com/avatar/fe5155fb5e422693c07686ee21c96358?s=256&d=identicon&r=PG&f=1","display_name":"Legna","link":"https://stackoverflow.com/users/4326551/legna"},"score":0,"last_activity_date":1655829993,"creation_date":1655829993,"post_type":"answer","post_id":72704416,"content_license":"CC BY-SA 4.0","link":"https://stackoverflow.com/a/72704416"}],"has_more":true,"backoff":10,"quota_max":300,"quota_remaining":194}
And when my app tries to get the information it doesn't work because it says:
expected begin_array but was begin_object at line 1 column 2 path $
How do I reach the arraw that is inside "items" because there is what I want my app to show?