Android Retrofit 2 custom callback

Updated 8 May 2017

Save

Retrofit 2 is one the best library I have used it so far. There are many cases in which we can use custom callback for our response callback such as Error Handling, Response Handling, Custom Response and much more.

By using a custom callback or any piece of custom code in an application we are promoting object oriented design pattern and thus reducing your efforts that are needed/mandatory to follow if not using it.

Here is an example of Custom Callback that is used to handle error , hide dialog  and return response data to original callback.

 

How it is helpful?

By using Custom Callback, we are preventing from extra effort required to handle error in each request. Plus, we can perform custom operation that are required whenver the response is recieved. In this case we are just hidding the AlertDialog.

We can use this callback instead of our existing callback. e.g.

And Here is the original callback which gets called from our Custom RetrofitCallback

 

That’s all it take for laziness!

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home