Even if this thread has accepted answer, feel free to propose other ideas, you do use or like
I’ve met these articles:
And that lead me to this Google I/O 2010 video about REST client applications
Since now, I’ve been creating REST component as static component in my Application controller class.
From now, I think, I should change the pattern. Somebody pointed out that Google IOSched application is great sample of how to write REST clients on Android. Somebody else told that this ways is too overcomplicated.
So, can anybody please show us what is the best practice? In short and simple way.
The IOSched application is too complex for sample use-case.
EDIT 2 (October 2017):
It is 2017. Just use Retrofit. There is almost no reason to use anything else.
EDIT:
The original answer is more than a year and a half old at the time of this edit. Although the concepts presented in original answer still hold, as other answers point out, there are now libraries out there that make this task easier for you. More importantly, some of these libraries handle device configuration changes for you.
The original answer is retained below for reference. But please also take the time to examine some of the Rest client libraries for Android to see if they fit your use cases. The following is a list of some of the libraries I’ve evaluated. It is by no means intended to be an exhaustive list.
Original Answer:
Presenting my approach to having REST clients on Android. I do not claim it is the best though 🙂 Also, note that this is what I came up with in response to my requirement. You might need to have more layers/add more complexity if your use case demands it. For example, I do not have local storage at all; because my app can tolerate loss of a few REST responses.
My approach uses just
AsyncTasks under the covers. In my case, I "call" these Tasks from myActivityinstance; but to fully account for cases like screen rotation, you might choose to call them from aServiceor such.I consciously chose my REST client itself to be an API. This means, that the app which uses my REST client need not even be aware of the actual REST URL’s and the data format used.
The client would have 2 layers:
Top layer: The purpose of this layer is to provide methods which mirror the functionality of the REST API. For example, you could have one Java method corresponding to every URL in your REST API (or even two – one for GETs and one for POSTs).
This is the entry point into the REST client API. This is the layer the app would use normally. It could be a singleton, but not necessarily.
The response of the REST call is parsed by this layer into a POJO and returned to the app.
This is the lower level
AsyncTasklayer, which uses HTTP client methods to actually go out and make that REST call.In addition, I chose to use a Callback mechanism to communicate the result of the
AsyncTasks back to the app.Enough of text. Let’s see some code now. Lets take a hypothetical REST API URL – http://myhypotheticalapi.com/user/profile
The top layer might look like this:
Note that the app doesn’t use the JSON or XML (or whatever other format) returned by the REST API directly. Instead, the app only sees the bean
Profile.Then, the lower layer (AsyncTask layer) might look like this:
Here’s how an app might use the API (in an
ActivityorService):I hope the comments are sufficient to explain the design; but I’d be glad to provide more info.