简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM