0

I have a response class - MyResponse as below to call the server using Retrofit2. resource is an array of objects.

public class MyResponse {
@SerializedName("success")
@Expose
private Boolean success;

@SerializedName("resource")
@Expose
private Array[] resource;

public ApiResponse(Boolean done, Array[] resource) {
    this.done = done;
    this.resource = resource;
}
//getters and setters
}

In the Activity, I created an Array as below:

MyResponse decodedResponse = response.body();
Array[] catsList = decodedResponse.getResource();

And I have a Model class Category as below:

public class Category {
    @SerializedName("id")
    @Expose
    private Integer id;

    @SerializedName("category")
    @Expose
    private String category;

    //getters, setter and constructor
}

Each object of above catsList array follows above category model(with id and category keys). Now how I make an array of Category (Category[]) from above catsList array?

I can't do it as Category[] catsList = decodedResponse.getObject(); because I want to create more arrays for other models too. Therefore I am planning to create a common MyResponse class as above and cast it's generated array to specific model when needed.

Also following method(after changed Array[] =to=> Object[] in MyResponse) is not supported. It gives java.lang.ClassCastException: java.lang.Object[] cannot be cast to my.package.Category[] exception.

Object[] catsList = decodedResponse.getObject();
Category[] catsListCooked = (Category[]) catsList;

EDIT: My JSON response from server has same style for every request types and uses one model for one request type. Because Retrofit wants to know which model class will use for the call in order to make a call as below.

@GET()
Call<MyResponse> getCategories(@Url String url);

Note: Please correct me if I can do this in another way. Because I call as mentioned above, I need to have a class in order to make a call.

So what I want to do is create a main response(MyResponse as above) which will receive the response and then use each model to fetch received data according to the request type. It is not possible to use the model directly when making a call because I have a custom JSON response from server.

i.e. If used model directly, then it gives errors such as expected BEGIN_ARRAY but provide BEGIN_OBJECT or expected BEGIN_OBJECT but provide BEGIN_ARRAY.

As commented below, from suggested duplicates, this suggestion by ytg looks like ok to achieve the solution. But using Arrays.copyOf() as suggested there, it seems it is not creating Category[] array to be useful since it gives "...cannot be stored in destination array of type..." error. Also other solutions from that question are not doable in my case.

Community
  • 1
  • 1
Janaka R Rajapaksha
  • 3,585
  • 1
  • 25
  • 28
  • 1
    @Abdelhak I don't need to try that to see that it won't work: a `Category[]` and a `Category` is not the same and not assignment compatible. – glglgl May 04 '16 at 12:03
  • 1
    Possible duplicate of: http://stackoverflow.com/questions/1115230/casting-object-array-to-integer-array-error – ytg May 04 '16 at 12:05
  • 2
    here is your answer: http://stackoverflow.com/questions/395030/quick-java-question-casting-an-array-of-objects-into-an-array-of-my-intended-cl – BT9 May 04 '16 at 12:07

0 Answers0