Martin
Martin

Reputation: 24308

Android: Separation of Rest API calling code, inside AsyncTask or separate?

I wondering if anyone can offer some suggestions on where I should be putting API Rest calling code. I of course could put this in every single AsyncTask that I am using. Which works but it doesn't make it very usable.

The other way I thought was creating an ApiConsumer class that would have all the rest calls list "getList()", "getExpiredItems()", "deleteItem(int id)" etc.

I though this would be ideal so they would be able to be used from everywhere. Inside these methods I am still using HttpUrlConnection to make network calls so I would still need an AsyncTask but instead of implementing the HttpUrlConnection stuff inside the AsyncTask then I would call the "ApiConsumer" methods that I setup before.

In my mind this seems a logical way of going, is it?

Am I trying to do something that has already has a different solution?

Anyone know of any articles or examples outlining similar solutions as I can't seem to find one.

I know placing HttpUrlConnection code directly inside the AsyncTask is ok, but I just don't see it very reusable and wanted to abstract it out.

Upvotes: 1

Views: 336

Answers (1)

Akhil
Akhil

Reputation: 6697

Retrofit is something you should look at, it pretty easy to integrate, will save lots of development time.

  1. Easy syntax,Support annotations
  2. In built GSON deserializer
  3. Customizable according to your needs
  4. Content format Agnostic
  5. Lots of devs are using it, so you will get good support over web

Volley & AsyncHttp are also good alternatives, you will find plenty of such libraries.

P.S: I am using retrofit for most of my projects

Upvotes: 1

Related Questions