【发布时间】:2019-04-25 17:39:06
【问题描述】:
我正在使用 MVVM 架构通过在 android studio 中进行改造来实现 Web 服务。我已经在我的视图类中处理了服务的响应。但我面临的问题是如何处理异常并将它们传递给我的视图类。一种方法是在我的 Bean 类中创建构造函数并将响应和错误都传递给它并更新 UI。但我想要更优化的方式来处理 UI 内的异常。
这是我的存储库代码:
final MutableLiveData<MyBeanClass> myBeanClass = new MutableLiveData<>();
ApiInterface apiInterface = ApiClient.getClientAuthentication().create(ApiInterface.class);
Call<MyBeanClass> call = apiInterface.getData(id);
call.enqueue(new Callback<MyBeanClass>() {
@Override
public void onResponse(Call<MyBeanClass> call, Response<MyBeanClass> response) {
if(response.body()!=null) {
myBeanClass.setValue(response.body());
}
}
@Override
public void onFailure(Call<MyBeanClass> call, Throwable t) {
//How to handle exceptions here and pass the exception to UI without making constructor in bean class
}
});
return myBeanClass;
【问题讨论】:
-
另一种方法是获取另一个实时数据的错误,将错误设置为它并在您的 UI 上观察它。
-
你能通过一些代码解释一下吗?
-
嗨,Ashmeet,你是怎么处理的?我也面临同样的问题,不确定处理不同错误+异常发生的过程
标签: android mvvm exception-handling retrofit