shaithana
shaithana

Reputation: 2490

Add more types in Ion Response

Using Ion by Koush, I need to make a JSON request that I cast directly into type, this way:

public interface UserCallback extends FutureCallback<Response<User>> {}

Builders.Any.B builder;

TypeToken<User> userType = new TypeToken<User>() {}

void userRequest(Context ctx, UserCallback callback) {
    builder = Ion.with(context).load("GET", "https://myendpoi.nt/");
    builder.as(userType).withResponse().setCallback(callback);
}

userRequest(getApplicationContext(),
    new UserCallback() {
        @Override
        public void onCompleted(Exception ex, Response<User> result) {

            // Cast my spell with User object...

        }
    });

The problems is: sometime the server responds with a JSON that cannot be reflected in the User class, but need to go in another object (such as Pet.class).

Well, I know I can get the response as a generic JsonObject and cast it subsequently, but my question is: there is a more elegant way to do this? It's possible add 2 Response in FutureCallback? so I can do such a thing:

public interface UserCallback extends FutureCallback<Response<User>, Response<Pet>> {}

or something else?

Many thanks.

Upvotes: 0

Views: 79

Answers (1)

arjun
arjun

Reputation: 3574

extend User class in Pet class or the other way and use the base class in FutureCallback. Then cast to whatever class need later in response.

Upvotes: 0

Related Questions