0

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?

  • Not sure how this android/java stuff works but it looks like you missed that the response is always an object with a property "item" and that is an Array/List. It looks your code assumes you get an array immediately but that is not correct. This mistake would explain the error as well. – rene Jun 21 '22 at 17:28
  • Do you know how to fix it @rene – Miguel Carvalho Jun 21 '22 at 17:58
  • Maybe this StackApps post https://stackapps.com/questions/4604/stackexchange-api-java-wrapper and the Github repository it links to can help. – rene Jun 21 '22 at 19:35

0 Answers0