如何使用Android中的可变实时数据从数据存储库中继改造响应以查看模型?

时间:2019-01-19 22:10:24

标签: android retrofit2 android-livedata mutablelivedata

我能够发出网络请求并在我的数据存储库中取回响应,但不能在我的视图模型中获得该响应。

数据存储库:

public class DataRepository {

private APIService apiService;
private static DataRepository INSTANCE = null;

public MutableLiveData<ResponseEntity> loginUser(UserEntity userEntity){

    final MutableLiveData<ResponseEntity> responseEntity = new MutableLiveData<>();
    apiService.loginUser(userEntity)
            .enqueue(new Callback<ResponseEntity>() {
                @Override
                public void onResponse(Call<ResponseEntity> call, Response<ResponseEntity> response) {
                    Log.d(Constants.LOGGER, "from data repository " + response.body());
                    responseEntity.setValue(response.body());
                }

                @Override
                public void onFailure(Call<ResponseEntity> call, Throwable t) {
                    Log.d(Constants.LOGGER, "from data repository: there was an error");
                    responseEntity.setValue(null);
                }
            });
    return responseEntity;
}
}

查看模型:

public class LoginViewModel extends AndroidViewModel {  

private MutableLiveData<ResponseEntity> networkResponse;


public void sendLoginNetworkRequest(UserEntity userEntity){
    networkResponse = mRepository.loginUser(userEntity);
}

public MutableLiveData<ResponseEntity> getResponse(){
    return networkResponse;
} 

活动:

public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    loginViewModel = ViewModelProviders.of(this).get(LoginViewModel.class);
        loginViewModel.getResponse()
            .observe(this, new Observer<ResponseEntity>() {
                @Override
                public void onChanged(@Nullable ResponseEntity responseEntity) {
                    Log.d(Constants.LOGGER, "response entity changed " + responseEntity);
                }
            });
}

    public void loginClicked(View view) {
       loginViewModel.sendLoginNetworkRequest(userEntity);
   }
}

显示来自数据存储库的日志,但没有显示来自活动的日志。我在做什么错了?

1 个答案:

答案 0 :(得分:0)

我找到了答案! 我必须将DataRepository类中的responseEntity MutableLiveData变量变成一个类变量,并创建一个返回该函数的函数,现在它可以工作了!

存储库:

public class DataRepository {

private APIService apiService;
private static DataRepository INSTANCE = null;
final MutableLiveData<ResponseEntity> responseEntity = new MutableLiveData<>();

public void loginUser(UserEntity userEntity){

    apiService.loginUser(userEntity)
            .enqueue(new Callback<ResponseEntity>() {
                @Override
                public void onResponse(Call<ResponseEntity> call, Response<ResponseEntity> response) {
                    Log.d(Constants.LOGGER, "from data repository " + response.body());
                    responseEntity.setValue(response.body());
                }

                @Override
                public void onFailure(Call<ResponseEntity> call, Throwable t) {
                    Log.d(Constants.LOGGER, "from data repository: there was an error");
                    responseEntity.setValue(null);
                }
            });
}
public MutableLiveData<ResponseEntity> getLiveResponses(){
        return responseEntity;
    }
}

Viewmodel:

public class LoginViewModel extends AndroidViewModel {      

public void sendLoginNetworkRequest(UserEntity userEntity){
    mRepository.loginUser(userEntity);
}

public MutableLiveData<ResponseEntity> getResponse(){
    return mRepository.getLiveResponse;
} 
}