Here is my definition:
public abstract class APICallback<T extends CommonModel.APIDataModel> implements Callback<CommonModel<T>>
I found that I can not convert APICallback back to Callback.
What is the problem?
How can I do it correctly?
Thank you!
----Update----
In fact I am using retrofit , I defined APICallback
implements retrofit.Callback
Here is the exact code :
public abstract class APICallback<T extends CommonModel.APIDataModel> implements Callback<CommonModel<T>> {
private Context context;
public APICallback(Context context) {
this.context = context;
}
public abstract void onResponse(T response);
@Override
public void onResponse(Response<CommonModel<T>> response, Retrofit retrofit) {
T data = response.body().data;
if (response.body().isSuccess()) {
onResponse(data);
onEnd();
} else {
dispatchError(response.body());
}
}
@Override
public void onFailure(Throwable t) {
LogUtils.d("Network error or exception", t.getMessage());
ViewUtils.showMessage(t.getMessage());
onEnd();
}
public void dispatchError(CommonModel<T> response) {
LogUtils.d("API error", response.toString());
ViewUtils.showMessage(response.data.msg);
onEnd();
}
public void onEnd() {
if (context instanceof BaseActivity) {
((BaseActivity) context).getLoading().hide();
}
}
I am using it like this:
getClient().login(u, p).enqueue(new APICallback<UserModel>(this) {
@Override
public void onResponse(UserModel response) {
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
});
enqueue :
void enqueue(Callback<T> callback);
login :
Call<UserModel> login(@Field("user_name") String userName, @Field("user_password") String userPassword);
The compiler tell me that can not convert anonymous APICallback<UserModel>
to Callback<UserModel>
I am confused.
I didn't notice that my login method definition doesn't match Callback<CommonModel<T>>
.
After I change it to
Call<CommonModel<UserModel>> login(@Field("user_name") String userName, @Field("user_password") String userPassword);
everything is ok.
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.