apradillap
apradillap

Reputation: 83

Model parsed with Retrofit

I have this JSON Response that I want to parse with Model Retrofit

[{"category":{"id":31,"name":"App's."}},
 {"category":{"id":32,"name":"Reinvention"}}]

With http://www.jsonschema2pojo.org/ I get these models:

public class Category {
    private Integer id;
    private String name;
}

public class CategoryResponse {
    private Category category;
}

And this is my Interface:

public interface Api {
    @GET("/categories.json")
    void getCategories(Callback <CategoryResponse> callback);
}

But the next call not works:

RestClient.get().getCategories(new Callback<CategoryResponse>() {
            @Override
            public void success(CategoryResponse categoryResponse, Response response) {

            }

            @Override
            public void failure(RetrofitError error) {

            }
        });

Does anyone know how to parsed with Model Retrofit?

Upvotes: 0

Views: 1031

Answers (1)

SnyersK
SnyersK

Reputation: 1296

Do you have acces to the code that generates the Json response? Because it should be something like this:

{Category:[{"id":31,"name":"App's."}, 
    {"id":32,"name":"Reinvention"}]}

Then you just have to adjust your CategoryResponse class to use a List

public class CategoryResponse {
    private List<Category> category;
}

Even simpler would be to adjust your Json to

[{"id":31,"name":"App's."}, 
    {"id":32,"name":"Reinvention"}]

Then you only need your Category class and use this in your interface

@GET("/categories.json")
void getCategories(Callback <List<Category>> callback);

EDIT

If you can't change the Json, you should be able to parse it by using List<CategoryResponse> like @SorryForMyEnglish suggested.

then your interface will look like this

@GET("/categories.json")
void getCategories(Callback <List<CategoryResponse>> callback);

Upvotes: 1

Related Questions