如何在 mvvm android 中处理异常并将该异常传递给我们的视图
How to handle exception in mvvm android and pass that exception to our view
我在 android studio 中使用 MVVM 架构通过改造来访问 Web 服务。在我看来,我已经处理了服务的响应 class。但是我面临的问题是如何处理异常并将它们传递给我的视图class。一种方法是在我的 Bean class 中创建构造函数并将响应和错误都传递给它并更新 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;
您可以将 Bean class 包装在通用资源 class 中并观察它。 Google 在他们的文档中提到了它:
public class Resource<T> {
@NonNull public final Status status;
@Nullable public final T data;
@Nullable public final String message;
private Resource(@NonNull Status status, @Nullable T data,
@Nullable String message) {
this.status = status;
this.data = data;
this.message = message;
}
public static <T> Resource<T> success(@NonNull T data) {
return new Resource<>(Status.SUCCESS, data, null);
}
public static <T> Resource<T> error(String msg, @Nullable T data) {
return new Resource<>(Status.ERROR, data, msg);
}
public static <T> Resource<T> loading(@Nullable T data) {
return new Resource<>(Status.LOADING, data, null);
}
public enum Status { SUCCESS, ERROR, LOADING }
}
可以在这里找到更多内容:Google Docs
这样做:
final MutableLiveData<Resource<MyBeanClass>> myBeanClass = new MutableLiveData<>();
ApiInterface apiInterface =
ApiClient.getClientAuthentication().create(ApiInterface.class);
Call<Response<MyBeanClass>> call = apiInterface.getData(id);
call.enqueue(new Callback<Resource<MyBeanClass>>() {
@Override
public void onResponse(Call<MyBeanClass> call,
Response<Resource<MyBeanClass>> response) {
if (response.body() != null) {
myBeanClass.setValue(Resource.success(response.body()));
}
}
@Override
public void onFailure(Call<MyBeanClass> call, Throwable t) {
myBeanClass.setValue(Resource.error(t.getMessage), null);
}
});
return myBeanClass;
现在同样地,观察您的 ViewModel Bean class 包裹在 Resource 中。
根据 Resource classes Status
处理成功和错误
我在 android studio 中使用 MVVM 架构通过改造来访问 Web 服务。在我看来,我已经处理了服务的响应 class。但是我面临的问题是如何处理异常并将它们传递给我的视图class。一种方法是在我的 Bean class 中创建构造函数并将响应和错误都传递给它并更新 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;
您可以将 Bean class 包装在通用资源 class 中并观察它。 Google 在他们的文档中提到了它:
public class Resource<T> {
@NonNull public final Status status;
@Nullable public final T data;
@Nullable public final String message;
private Resource(@NonNull Status status, @Nullable T data,
@Nullable String message) {
this.status = status;
this.data = data;
this.message = message;
}
public static <T> Resource<T> success(@NonNull T data) {
return new Resource<>(Status.SUCCESS, data, null);
}
public static <T> Resource<T> error(String msg, @Nullable T data) {
return new Resource<>(Status.ERROR, data, msg);
}
public static <T> Resource<T> loading(@Nullable T data) {
return new Resource<>(Status.LOADING, data, null);
}
public enum Status { SUCCESS, ERROR, LOADING }
}
可以在这里找到更多内容:Google Docs
这样做:
final MutableLiveData<Resource<MyBeanClass>> myBeanClass = new MutableLiveData<>();
ApiInterface apiInterface =
ApiClient.getClientAuthentication().create(ApiInterface.class);
Call<Response<MyBeanClass>> call = apiInterface.getData(id);
call.enqueue(new Callback<Resource<MyBeanClass>>() {
@Override
public void onResponse(Call<MyBeanClass> call,
Response<Resource<MyBeanClass>> response) {
if (response.body() != null) {
myBeanClass.setValue(Resource.success(response.body()));
}
}
@Override
public void onFailure(Call<MyBeanClass> call, Throwable t) {
myBeanClass.setValue(Resource.error(t.getMessage), null);
}
});
return myBeanClass;
现在同样地,观察您的 ViewModel Bean class 包裹在 Resource 中。 根据 Resource classes Status
处理成功和错误