简体   繁体   English

在离子响应中添加更多类型

[英]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: 使用Koush的Ion ,我需要发出一个直接转换为typeJSON请求,方法是:

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 ). 问题是:有时服务器使用无法反映在User类中但需要放入另一个对象(例如Pet.class )的JSON响应。

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? 好吧,我知道我可以将响应作为通用JsonObject并随后进行转换,但是我的问题是:还有一种更优雅的方法吗? It's possible add 2 Response in FutureCallback ? 可以在FutureCallback添加2 Response吗? 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 . Pet类或其他方法中扩展User类,并在FutureCallback使用基类。 Then cast to whatever class need later in response. 然后,将其转换为以后需要的任何类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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