exception handling through mvvm android
I am using MVVM architecture to hit a web service through retrofit in android studio. I have handled the response of the service in my view class. But the problem i am facing is how to handle the exceptions and pass them to my view class. One way is to make constructor in my Bean class and pass both the response and error to it and update UI. But i want more optimised way to handle exceptions inside UI.
Here is my repository code :
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;
android mvvm exception-handling retrofit
add a comment |
I am using MVVM architecture to hit a web service through retrofit in android studio. I have handled the response of the service in my view class. But the problem i am facing is how to handle the exceptions and pass them to my view class. One way is to make constructor in my Bean class and pass both the response and error to it and update UI. But i want more optimised way to handle exceptions inside UI.
Here is my repository code :
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;
android mvvm exception-handling retrofit
One other way is to take another live data for error, set your error to it and observe it on your UI.
– Jeel Vankhede
Nov 23 '18 at 5:34
Can u explain it via some code?
– Ashmeet Arora
Nov 23 '18 at 5:43
add a comment |
I am using MVVM architecture to hit a web service through retrofit in android studio. I have handled the response of the service in my view class. But the problem i am facing is how to handle the exceptions and pass them to my view class. One way is to make constructor in my Bean class and pass both the response and error to it and update UI. But i want more optimised way to handle exceptions inside UI.
Here is my repository code :
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;
android mvvm exception-handling retrofit
I am using MVVM architecture to hit a web service through retrofit in android studio. I have handled the response of the service in my view class. But the problem i am facing is how to handle the exceptions and pass them to my view class. One way is to make constructor in my Bean class and pass both the response and error to it and update UI. But i want more optimised way to handle exceptions inside UI.
Here is my repository code :
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;
android mvvm exception-handling retrofit
android mvvm exception-handling retrofit
asked Nov 23 '18 at 5:31
Ashmeet AroraAshmeet Arora
426
426
One other way is to take another live data for error, set your error to it and observe it on your UI.
– Jeel Vankhede
Nov 23 '18 at 5:34
Can u explain it via some code?
– Ashmeet Arora
Nov 23 '18 at 5:43
add a comment |
One other way is to take another live data for error, set your error to it and observe it on your UI.
– Jeel Vankhede
Nov 23 '18 at 5:34
Can u explain it via some code?
– Ashmeet Arora
Nov 23 '18 at 5:43
One other way is to take another live data for error, set your error to it and observe it on your UI.
– Jeel Vankhede
Nov 23 '18 at 5:34
One other way is to take another live data for error, set your error to it and observe it on your UI.
– Jeel Vankhede
Nov 23 '18 at 5:34
Can u explain it via some code?
– Ashmeet Arora
Nov 23 '18 at 5:43
Can u explain it via some code?
– Ashmeet Arora
Nov 23 '18 at 5:43
add a comment |
2 Answers
2
active
oldest
votes
Check out how it can be done from your repository code :
//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);
}
});
}
From your ViewModel
Class, make a method that calls your repo API method and another to provide your live data to be observed on your UI.
Hope it helps !.
yes i understood it till repository..but i am facing problems in view model...can u please tell me about how to code in view model to get two different types of data i.e. error/response
– Ashmeet Arora
Feb 7 at 6:44
Actually answer is pretty old, recently i get to know about how we can wrap both things from here: developer.android.com/jetpack/docs/guide#addendum. Let me know if it satisfies your case, so that i'll edit my answer accordingly.
– Jeel Vankhede
Feb 7 at 6:47
i am unable to get a solution from this link..i want to get the name of that particular exception which i am getting in my repository class to my view through view model..and i have taken the response from my repo class through view model to my view..but how it can be done in case of any exception..how to take that exception to my view........
– Ashmeet Arora
Feb 7 at 6:52
add a comment |
Instead of creating two Mutable classes.
You can just create a wrapper object for both error and success state or even loading state
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)
}
}
}
And then use MutableLive data as this type
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;
You can check out this google sample
https://github.com/googlesamples/android-architecture-components/tree/master/GithubBrowserSample
yeah i did this..now my problem is how to handle the exception in the UI..?
– Ashmeet Arora
Nov 26 '18 at 5:58
put an integer as a property in your object to indicate the status of the resource object (Mybeanclass) for example if the resource is success it will be 1 if the resource is fail it will be 0 in that case u can check the object integer value and whether it is 1 or 0 and handle it
– Kaung Khant Thu
Nov 27 '18 at 3:51
but my point is how to get the name of particular exception from the repository to the UI?
– Ashmeet Arora
Nov 27 '18 at 9:26
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441086%2fexception-handling-through-mvvm-android%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Check out how it can be done from your repository code :
//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);
}
});
}
From your ViewModel
Class, make a method that calls your repo API method and another to provide your live data to be observed on your UI.
Hope it helps !.
yes i understood it till repository..but i am facing problems in view model...can u please tell me about how to code in view model to get two different types of data i.e. error/response
– Ashmeet Arora
Feb 7 at 6:44
Actually answer is pretty old, recently i get to know about how we can wrap both things from here: developer.android.com/jetpack/docs/guide#addendum. Let me know if it satisfies your case, so that i'll edit my answer accordingly.
– Jeel Vankhede
Feb 7 at 6:47
i am unable to get a solution from this link..i want to get the name of that particular exception which i am getting in my repository class to my view through view model..and i have taken the response from my repo class through view model to my view..but how it can be done in case of any exception..how to take that exception to my view........
– Ashmeet Arora
Feb 7 at 6:52
add a comment |
Check out how it can be done from your repository code :
//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);
}
});
}
From your ViewModel
Class, make a method that calls your repo API method and another to provide your live data to be observed on your UI.
Hope it helps !.
yes i understood it till repository..but i am facing problems in view model...can u please tell me about how to code in view model to get two different types of data i.e. error/response
– Ashmeet Arora
Feb 7 at 6:44
Actually answer is pretty old, recently i get to know about how we can wrap both things from here: developer.android.com/jetpack/docs/guide#addendum. Let me know if it satisfies your case, so that i'll edit my answer accordingly.
– Jeel Vankhede
Feb 7 at 6:47
i am unable to get a solution from this link..i want to get the name of that particular exception which i am getting in my repository class to my view through view model..and i have taken the response from my repo class through view model to my view..but how it can be done in case of any exception..how to take that exception to my view........
– Ashmeet Arora
Feb 7 at 6:52
add a comment |
Check out how it can be done from your repository code :
//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);
}
});
}
From your ViewModel
Class, make a method that calls your repo API method and another to provide your live data to be observed on your UI.
Hope it helps !.
Check out how it can be done from your repository code :
//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);
}
});
}
From your ViewModel
Class, make a method that calls your repo API method and another to provide your live data to be observed on your UI.
Hope it helps !.
answered Nov 23 '18 at 5:54
Jeel VankhedeJeel Vankhede
2,5772420
2,5772420
yes i understood it till repository..but i am facing problems in view model...can u please tell me about how to code in view model to get two different types of data i.e. error/response
– Ashmeet Arora
Feb 7 at 6:44
Actually answer is pretty old, recently i get to know about how we can wrap both things from here: developer.android.com/jetpack/docs/guide#addendum. Let me know if it satisfies your case, so that i'll edit my answer accordingly.
– Jeel Vankhede
Feb 7 at 6:47
i am unable to get a solution from this link..i want to get the name of that particular exception which i am getting in my repository class to my view through view model..and i have taken the response from my repo class through view model to my view..but how it can be done in case of any exception..how to take that exception to my view........
– Ashmeet Arora
Feb 7 at 6:52
add a comment |
yes i understood it till repository..but i am facing problems in view model...can u please tell me about how to code in view model to get two different types of data i.e. error/response
– Ashmeet Arora
Feb 7 at 6:44
Actually answer is pretty old, recently i get to know about how we can wrap both things from here: developer.android.com/jetpack/docs/guide#addendum. Let me know if it satisfies your case, so that i'll edit my answer accordingly.
– Jeel Vankhede
Feb 7 at 6:47
i am unable to get a solution from this link..i want to get the name of that particular exception which i am getting in my repository class to my view through view model..and i have taken the response from my repo class through view model to my view..but how it can be done in case of any exception..how to take that exception to my view........
– Ashmeet Arora
Feb 7 at 6:52
yes i understood it till repository..but i am facing problems in view model...can u please tell me about how to code in view model to get two different types of data i.e. error/response
– Ashmeet Arora
Feb 7 at 6:44
yes i understood it till repository..but i am facing problems in view model...can u please tell me about how to code in view model to get two different types of data i.e. error/response
– Ashmeet Arora
Feb 7 at 6:44
Actually answer is pretty old, recently i get to know about how we can wrap both things from here: developer.android.com/jetpack/docs/guide#addendum. Let me know if it satisfies your case, so that i'll edit my answer accordingly.
– Jeel Vankhede
Feb 7 at 6:47
Actually answer is pretty old, recently i get to know about how we can wrap both things from here: developer.android.com/jetpack/docs/guide#addendum. Let me know if it satisfies your case, so that i'll edit my answer accordingly.
– Jeel Vankhede
Feb 7 at 6:47
i am unable to get a solution from this link..i want to get the name of that particular exception which i am getting in my repository class to my view through view model..and i have taken the response from my repo class through view model to my view..but how it can be done in case of any exception..how to take that exception to my view........
– Ashmeet Arora
Feb 7 at 6:52
i am unable to get a solution from this link..i want to get the name of that particular exception which i am getting in my repository class to my view through view model..and i have taken the response from my repo class through view model to my view..but how it can be done in case of any exception..how to take that exception to my view........
– Ashmeet Arora
Feb 7 at 6:52
add a comment |
Instead of creating two Mutable classes.
You can just create a wrapper object for both error and success state or even loading state
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)
}
}
}
And then use MutableLive data as this type
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;
You can check out this google sample
https://github.com/googlesamples/android-architecture-components/tree/master/GithubBrowserSample
yeah i did this..now my problem is how to handle the exception in the UI..?
– Ashmeet Arora
Nov 26 '18 at 5:58
put an integer as a property in your object to indicate the status of the resource object (Mybeanclass) for example if the resource is success it will be 1 if the resource is fail it will be 0 in that case u can check the object integer value and whether it is 1 or 0 and handle it
– Kaung Khant Thu
Nov 27 '18 at 3:51
but my point is how to get the name of particular exception from the repository to the UI?
– Ashmeet Arora
Nov 27 '18 at 9:26
add a comment |
Instead of creating two Mutable classes.
You can just create a wrapper object for both error and success state or even loading state
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)
}
}
}
And then use MutableLive data as this type
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;
You can check out this google sample
https://github.com/googlesamples/android-architecture-components/tree/master/GithubBrowserSample
yeah i did this..now my problem is how to handle the exception in the UI..?
– Ashmeet Arora
Nov 26 '18 at 5:58
put an integer as a property in your object to indicate the status of the resource object (Mybeanclass) for example if the resource is success it will be 1 if the resource is fail it will be 0 in that case u can check the object integer value and whether it is 1 or 0 and handle it
– Kaung Khant Thu
Nov 27 '18 at 3:51
but my point is how to get the name of particular exception from the repository to the UI?
– Ashmeet Arora
Nov 27 '18 at 9:26
add a comment |
Instead of creating two Mutable classes.
You can just create a wrapper object for both error and success state or even loading state
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)
}
}
}
And then use MutableLive data as this type
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;
You can check out this google sample
https://github.com/googlesamples/android-architecture-components/tree/master/GithubBrowserSample
Instead of creating two Mutable classes.
You can just create a wrapper object for both error and success state or even loading state
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)
}
}
}
And then use MutableLive data as this type
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;
You can check out this google sample
https://github.com/googlesamples/android-architecture-components/tree/master/GithubBrowserSample
answered Nov 23 '18 at 8:03
Kaung Khant ThuKaung Khant Thu
256
256
yeah i did this..now my problem is how to handle the exception in the UI..?
– Ashmeet Arora
Nov 26 '18 at 5:58
put an integer as a property in your object to indicate the status of the resource object (Mybeanclass) for example if the resource is success it will be 1 if the resource is fail it will be 0 in that case u can check the object integer value and whether it is 1 or 0 and handle it
– Kaung Khant Thu
Nov 27 '18 at 3:51
but my point is how to get the name of particular exception from the repository to the UI?
– Ashmeet Arora
Nov 27 '18 at 9:26
add a comment |
yeah i did this..now my problem is how to handle the exception in the UI..?
– Ashmeet Arora
Nov 26 '18 at 5:58
put an integer as a property in your object to indicate the status of the resource object (Mybeanclass) for example if the resource is success it will be 1 if the resource is fail it will be 0 in that case u can check the object integer value and whether it is 1 or 0 and handle it
– Kaung Khant Thu
Nov 27 '18 at 3:51
but my point is how to get the name of particular exception from the repository to the UI?
– Ashmeet Arora
Nov 27 '18 at 9:26
yeah i did this..now my problem is how to handle the exception in the UI..?
– Ashmeet Arora
Nov 26 '18 at 5:58
yeah i did this..now my problem is how to handle the exception in the UI..?
– Ashmeet Arora
Nov 26 '18 at 5:58
put an integer as a property in your object to indicate the status of the resource object (Mybeanclass) for example if the resource is success it will be 1 if the resource is fail it will be 0 in that case u can check the object integer value and whether it is 1 or 0 and handle it
– Kaung Khant Thu
Nov 27 '18 at 3:51
put an integer as a property in your object to indicate the status of the resource object (Mybeanclass) for example if the resource is success it will be 1 if the resource is fail it will be 0 in that case u can check the object integer value and whether it is 1 or 0 and handle it
– Kaung Khant Thu
Nov 27 '18 at 3:51
but my point is how to get the name of particular exception from the repository to the UI?
– Ashmeet Arora
Nov 27 '18 at 9:26
but my point is how to get the name of particular exception from the repository to the UI?
– Ashmeet Arora
Nov 27 '18 at 9:26
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441086%2fexception-handling-through-mvvm-android%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
One other way is to take another live data for error, set your error to it and observe it on your UI.
– Jeel Vankhede
Nov 23 '18 at 5:34
Can u explain it via some code?
– Ashmeet Arora
Nov 23 '18 at 5:43