通过mvvm android进行异常处理

智慧并不产生于学历,而是来自对于知识的终生不懈的追求。这篇文章主要讲述通过mvvm android进行异常处理相关的知识,希望能为你提供帮助。
我正在使用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;

答案你可以在这里接口。从onFailure调用接口方法并从UI端提供该接口的实现,因此每当出现错误时,yiu将进入UI端
另一答案看看如何从您的存储库代码中完成它:
//Take it globally in your repository class, and provide getter for it. final MutableLiveData< MyBeanClass> myBeanClass = new MutableLiveData< > (); final MutableLiveData< Throwable> error = new MutableLiveData< > (); public void someApiCallMethod() { // In your method call for API 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()); } // Even you can handle your response error if it's in your response. }@Override public void onFailure(Call< MyBeanClass> call, Throwable t) { //Set your error live data from here error.setValue(t); } }); }

ViewModel类中,创建一个调用repo API方法的方法,另一个方法用于提供在UI上观察的实时数据。
希望能帮助到你 !。
另一答案而不是创建两个Mutable类。您可以为错误和成功状态甚至加载状态创建包装器对象
data class Resource< out T> (val status: Status, val data: T?, val message: String?) { companion object { fun < T> success(data: T?): Resource< T> { return Resource(SUCCESS, data, null) }fun < T> error(msg: String, data: T?): Resource< T> { return Resource(ERROR, data, msg) }fun < T> loading(data: T?): Resource< T> { return Resource(LOADING, data, null) } } }

然后使用MutableLive数据作为此类型
final MutableLiveData< Resource< 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(Resource< MyBeanClass> .success(response.body)); } }@Override public void onFailure(Call< MyBeanClass> call, Throwable t) { myBeanClass.setValue(Resource< MyBeanClass> .error(t.getLocalizedMessage())); } }); return myBeanClass;

【通过mvvm android进行异常处理】你可以查看这个谷歌样本https://github.com/googlesamples/android-architecture-components/tree/master/GithubBrowserSample

    推荐阅读