[英]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
,我需要发出一个直接转换为type
的JSON
请求,方法是:
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.