Is there a way to catch 400 to 599 http status in the onFail() method by using retrofit 2.0? Whenever the rest api returns 400,401 etc. then retrofit acting like 200 response and handle it on the onSuccess(). How can i achieve it?
Asked
Active
Viewed 289 times
1 Answers
3
Whenever the rest api returns 400,401 etc. then retrofit acting like 200 response
onFailure is invoked only in case of a network error, E.g. an IoException
in case you don't have connection. If the server is able to process your request, either in case of success or error onResponse
will be called. There you can check the status code of the request and act accordingly

Blackbelt
- 156,034
- 29
- 297
- 305
-
I dont want to check all onResponse method by writing if(response.body().status == 200). In retrofit 1.9 that was working good. What will i do for achive it? – aligur Jun 20 '16 at 08:33
-
1have a BaseAbstract class that does the check for you. Then use the Template pattern to call two different methods (both declared abstract in the base class). – Blackbelt Jun 20 '16 at 08:35
-
i couldnt use template pattern by using retrofit. Can u please put some code to achieve it? thanks – aligur Jun 20 '16 at 08:41