0

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?

aligur
  • 3,387
  • 3
  • 34
  • 51
  • 2
    Look at this question: http://stackoverflow.com/questions/31808083/how-to-get-retrofit-success-responce-status-codes – Olcay Ertaş Jun 20 '16 at 08:30

1 Answers1

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
  • 1
    have 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