如何使用 android 中的可变实时数据将改造响应从数据存储库中继到视图模型?
How to relay retrofit response from data repository to view model using mutable live data in android?
我能够发出网络请求并在我的数据存储库中取回响应,但无法在我的视图模型中获得响应。
数据存储库:
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;
}
Activity:
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);
}
}
数据存储库中的日志显示,但 activity 中的日志未显示。我做错了什么?
我找到答案了!
我必须将我的 DataRepository class 中的 responseEntity MutableLiveData 变量设为 class 变量并创建一个 returns 函数,现在它可以工作了!
存储库:
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;
}
}
我能够发出网络请求并在我的数据存储库中取回响应,但无法在我的视图模型中获得响应。
数据存储库:
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;
}
Activity:
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);
}
}
数据存储库中的日志显示,但 activity 中的日志未显示。我做错了什么?
我找到答案了! 我必须将我的 DataRepository class 中的 responseEntity MutableLiveData 变量设为 class 变量并创建一个 returns 函数,现在它可以工作了!
存储库:
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;
}
}