Retrofit 2.0 moved the ErrorHandler and used the new Callback , which includes two methods:
public void onResponse(Response<T> response, Retrofit retrofit)```` public void onFailure(Throwable t)
Retrofit2.x will receive the whole HTTP response in onResponse , although the http code is not 2xx or 3xx, here you need to check the response status code in your onResponse method and check whether the response matches the response (usually 2xx or 3xx) and perform the correct logical processing .
I updated retrofit2.x and my decision on centralized error handling: Creating an abstract class that extends Retrofit.Callback using the two methods onSuccess and onFailed, onFailed is not abstract, as I always do the same process when the business logic failed , and performs another task when the request is successful. You can refer to the sample code here
then when you need to send an HTTP request, you need to implement the onSuccess method, and in some cases you can also override the onFailed method, as I mentioned in my project, in most cases I handle a failed failure. You can reference the example here , which I used retrofit2 to send a message.
Hope this helps you!
source share