androidjsonretrofitretrofit2

I can't get JSON parsed into a list view (Pokeapi)


I'm trying to teach myself how to use REST APIs in Android Studio. I'm very new to Android Development as a whole and this will only be the second time that I've used a REST API. I have attempted to follow a few tutorials on YouTube but I'm still having issues and I feel like the solution is going to be very simple and I'm going to feel very stupid, especially since I'm using RetroFit... I just want to see the Pokemon names shown in the list view (for now at least).

Here are my code files

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ListView listView = (ListView) findViewById(R.id.listView);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Pokeapi.URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        Pokeapi pokeapi = retrofit.create(Pokeapi.class);


        Call<List<Pokemon>> call = pokeapi.getPokemonNameAndPic();

        call.enqueue(new Callback<List<Pokemon>>() {
            @Override
            public void onResponse(Call<List<Pokemon>> call, Response<List<Pokemon>> response) {
                List<Pokemon> pokemon = response.body();

                    String[] pokemonNames = new String[pokemon.size()];

                    for (int i = 0; i < pokemon.size(); i++) {

                        pokemonNames[i] = pokemon.get(i).getName();
                    }
                    listView.setAdapter(
                            new ArrayAdapter<String>(
                                    getApplicationContext(),
                                    android.R.layout.simple_list_item_1,
                                    pokemonNames
                            )
                    );

                }


            @Override
            public void onFailure(Call<List<Pokemon>> call, Throwable t) {
                Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });

    }
}

_

public interface Pokeapi {

String URL = "https://pokeapi.co/api/v2/";

@GET("pokemon")
Call<List<Pokemon>> getPokemonNameAndPic();

}

_

public class Pokemon {

private String url;
private String name;

public Pokemon(String url, String name) {
    this.url = url;
    this.name = name;
}

public String getUrl() {
    return url;
}

public String getName() {
    return name;
}
}

Any help is greatly appreciated! Thank you :)


Solution

  • You must write your model classes according to your json response.So in this case you should change your "Pokemon" class into :

     public class Data {
    
         @SerializedName("count")
         private Integer count;
         @SerializedName("previous")
         private Object previous;
         @SerializedName("results")
         private List<Pokemon> results = null;
         @SerializedName("next")
         private String next;
    
         public Integer getCount() {
            return count;
         }
    
         public void setCount(Integer count) {
           this.count = count;
         }
    
         public Object getPrevious() {
             return previous;
         }
    
        public void setPrevious(Object previous) {
            this.previous = previous;
        }
    
        public List<Pokemon> getResults() {
            return results;
        }
    
         public void setResults(List<Pokemon> results) {
            this.results = results;
        }
    
        public String getNext() {
            return next;
        }
    
         public void setNext(String next) {
            this.next = next;
        }
    
    
        public class Pokemon {
    
            @SerializedName("url")
            private String url;
            @SerializedName("name")
            private String name;
    
            public String getUrl() {
                 return url;
            }
    
            public void setUrl(String url) {
                this.url = url;
            }
    
            public String getName() {
                return name;
            }
    
              public void setName(String name) {
                this.name = name;
            }
    
        }
    
    }
    

    And you api interface as :

    public interface Pokeapi {
    
         String URL = "https://pokeapi.co/api/v2/";
    
        @GET("pokemon")
        Call<Data> getPokemonNameAndPic();
    
    }
    

    Update your mainActivity as :

    public class MainActivity extends AppCompatActivity {
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        final ListView listView = (ListView) findViewById(R.id.listView);
    
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Pokeapi.URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    
        Pokeapi pokeapi = retrofit.create(Pokeapi.class);
    
    
        Call<Data> call = pokeapi.getPokemonNameAndPic();
    
        call.enqueue(new Callback<Data>() {
            @Override
            public void onResponse(Call<Data> call, Response<Data> response) {
                Log.d("response", response.body().toString());
                Data data = response.body();
    
                    String[] pokemonNames = new String[data.getResults().size()];
    
                    for (int i = 0; i < data.getResults().size(); i++) {
    
                        pokemonNames[i] = data.getResults().get(i).getName();
                    }
                    for (String item : pokemonNames){
                        Log.d("item", item);
                    }
                    listView.setAdapter(
                            new ArrayAdapter<String>(
                                    getApplicationContext(),
                                    android.R.layout.simple_list_item_1,
                                    pokemonNames
                            )
                    );
    
                }
    
    
            @Override
            public void onFailure(Call<Data> call, Throwable t) {
                Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    
    }
    

    }