Я использую retrofit:2.1.0
, и я пытаюсь сохранить ответ, возвращенный моему собственному POJO (UserProfile), но я не могу получить доступ к POJO, которому я назначаю ответ, вне обратных вызовов.Сохранить асинхронный отклик
Итак, в этом призыве ниже, я хочу иметь доступ к UserProfile вне этого вызова.
//adding `UserProfile userProfile;` outside of Call didn't help either
call.enqueue(new Callback<UserProfile>() {
@Override
public void onResponse(Call<UserProfile> call, Response<UserProfile> response) {
if (response.isSuccessful()) {
UserProfile userProfile = response.body();
}
}
@Override
public void onFailure(Call<UserProfile> call, Throwable t) {
//do something
}
});
// здесь Userprofile равна нулю и, следовательно, не может получить статус Log.d (TAG, "Статус вне вызова является:" + userProfile.getStatus());
Новый подход, такой же результат
private List<UserProfile> userProfileList = new ArrayList<>();
call.enqueue(new Callback<UserProfile>() {
@Override
public void onResponse(Call<UserProfile> call, Response<UserProfile> response) {
if (response.isSuccessful()) {
UserProfile userProfile = response.body();
userProfileList.add(userProfile);
}
}
@Override
public void onFailure(Call<UserProfile> call, Throwable t) {
//do something
}
});
И это один увольняют еще до обратного вызова и, следовательно, является нулевым
if(userProfileList.size() > 0) {
for(UserProfile userProfile : userProfileList) {
Log.d(TAG, "Status is: " + userProfile.getStatus());
}
} else {
Log.d(TAG, "YakkerProfileList is NULL");
}
«Я не могу получить доступ к POJO, я назначаю ответ за пределами обратных вызовов». - Что именно вы хотите сделать? Вы получаете какую-либо ошибку? –
* Когда вы пытаетесь получить доступ к 'UserProfile'? Если вы попытаетесь получить к нему доступ до того, как будет вызван обратный вызов, он вернет «null». – Bryan
Да, за пределами 'call' я даже не могу получить доступ к' UserProfile'. Я попытался создать экземпляр «UserProfile», а затем получить доступ к 'userProfile = response.body()', и когда я сделаю getter так: 'Log.d (TAG,« Пользовательский статус вне Call: »+ userProfile.getStatus()); ' –