Pablo Rodrigo Alves da Silva Pablo Rodrigo Alves da Silva - 2 months ago 11
JSON Question

Retrofit returns data but the pojo object is always empty

I'm trying to consume a service by using retrofit 2. When I call it using a web browser or log the call using an interceptor, there're some results, but my pojo classes are always empty.

The json result content is similar to:

"similarartists": {
"artist": [
"name": "BarĂ£o Vermelho",
"mbid": "84ac395b-482b-48cb-b381-b9bc420b2dd3",
"match": "1",
"url": "",
"image": [],
"streamable": "0"
"@attr": {
"artist": "Cazuza"

And these are my pojo classes:

public class Similarartists {

private List<Artist> artist = new ArrayList<>();

private Attr attr;


public class Artist {

private String name;

private String mbid;

private String match;

private String url;

private List<Object> image = new ArrayList<>();

private String streamable;

public class Attr {
private String artist;

The service connection class:

public class ApiService {
private static final String BASE_URL = "";
public static final String API_KEY = "XXXXXXXXXX";

private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder()
.readTimeout(30, TimeUnit.SECONDS)
.connectTimeout(30, TimeUnit.SECONDS);

private static Retrofit.Builder builder =
new Retrofit.Builder()

public static <S> S createService(Class<S> serviceClass) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();


httpClient.addInterceptor(new Interceptor() {
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();

// Request customization: add request headers
Request.Builder requestBuilder = original.newBuilder()
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.method(original.method(), original.body());

Request request =;
return chain.proceed(request);

Retrofit retrofit = builder.client(;

return retrofit.create(serviceClass);


Ans this is my "button" action:

private void openSearchDialog() {
ArtistService artistService = ApiService.createService(ArtistService.class);
Call<Similarartists> call = artistService.getSimilar("nirvana", ApiService.API_KEY);
call.enqueue(new Callback<Similarartists>() {
public void onResponse(Call<Similarartists> call, Response<Similarartists> response) {
if (response.isSuccessful()) {
Log.i(TAG, "onResponse: " + response.body().toString());

public void onFailure(Call<Similarartists> call, Throwable t) {
Log.e(TAG, "onFailure: ", t);

The problem is response.body().getArtist() is always empty. Please help me.

And this's my log result:

-01 22:51:58.844 23843-24043/com.sample.pablo.hellomusic D/OkHttp: {"similarartists":{"artist":[{"name":"Hole","mbid":"1dcc8968-f2cd-441c-beda-6270f70f2863","match":"1","url":"","image":[{"#text":"","size":"small"},{"#text":"","size":"medium"},{"#text":"","size":"large"},{"#text":"","size":"extralarge"},{"#text":"","size":"mega"},{"#text":"","size":""}],"streamable":"0"}],"@attr":{"artist":"Nirvana"}}}


Since there is a top-level key of similarartists in the response JSON I think you need another wrapper around this object, like:

public class SimilarArtistsResponse {
  private Similarartists similars;

And its this object you would specify in your Call<SimilarArtistsResponse>