【问题标题】:How to relay retrofit response from data repository to view model using mutable live data in android?如何使用android中的可变实时数据将数据存储库的改造响应中继到视图模型?
【发布时间】:2019-06-13 18:32:36
【问题描述】:

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

数据存储库:

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

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

【问题讨论】:

    标签: android retrofit2 android-livedata mutablelivedata


    【解决方案1】:

    我找到了答案! 我必须将我的 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;
    } 
    }
    

    【讨论】:

      猜你喜欢
      • 2021-06-26
      • 2019-05-27
      • 1970-01-01
      • 2018-11-07
      • 2011-04-13
      • 1970-01-01
      • 2021-12-31
      • 2012-07-18
      • 1970-01-01
      相关资源
      最近更新 更多