0

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

资料库:

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);
   }
}

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

4

1 回答 1

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;
    }
}

视图模型:

public class LoginViewModel extends AndroidViewModel {      

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

public MutableLiveData<ResponseEntity> getResponse(){
    return mRepository.getLiveResponse;
} 
}
于 2019-01-20T14:02:14.957 回答