oyatek oyatek - 27 days ago 12
Android Question

Android: Service to communicate with server API

I'm using

AsyncHTTPClient
to communicate with my server API.

I've read that I have to use
services
to communicate with a server.

1) Is
AsyncHTTPClient
already a
service
? Or do I still need to put all methods which use
AsyncHTTPClient
into my own
services
? Or is it OK to call
AsyncHTTPClient
methods directly from my Activities?

2) I can't understand how
services
should be properly used. Let's say I don't use
AsyncHTTPClient
but I have few different actions which make a call to server API (get realities, get users, post user, etc.). Do I have to put each action into a separate
service
? If not, how can I call different methods from one same service? All examples I have seen always show that there's just one action which is automatically being called on service start up by Activity.

3) I found another Android REST library Retrofit - does it have any advantages over
AsyncHTTPClient
? Do I need to put it into
services
?

Answer

At first, I would recommend you to use Retrofit. I've already used AsynHttp, but now I am using Retrofit, and I am very satisfied with it.

About the topics in your question:

I've read that I have to use services to communicate with a server.

Where do you read this? I don't create Service to my requests, and all of then works pretty well. The main point when creating your requests is to make them in a background thread. So if you don't use some lib like AsyncHttp or Retrofit, you have to put your requests in an AsynTask class, for example.

Is AsyncHTTPClient already a service?

No. It is a library to make asynchronous HTTP requests. In the site of the lib, they say:

All requests are made outside of your app’s main UI thread and You can also use it in Service or background thread.

That is, you can use it in a Service, they do not say you have to use it in a Service.

The example in the site teaches how to make a RestClient with this library. It is a good example. You only need to create a static class and put the methods to make your posts, gets etc

I can't understand how services should be properly used.

Services are used for long-running operations in the background. A possible way to use a Service with your lib (AsyncHttp or Retrofit) is when you want to make requests to the server, even when the user is not using your app. So you can create a Service with a timer that will call the server at the intervals defined by you.

Retrofit - does it have any advantages over AsyncHTTPClient?

Backing to Retrofit, again you don't have to put it in a Service. Only if you need this. From threir site, they say: Retrofit turns your REST API into a Java interface. This make your client very simple, and self documented because each call to your server API will be a method in this interface, and I think this is a big advantage. It is simple to use Retrofit. As I said, now I changed to Retrofit, and I can say, the lib has a good documentation and support, it has many users. So it is easy to find solutions to some bug. Other advantage is that it already has the lib GSON, which is very useful to parse the JSONs that you will receive from your server API.

But of course talking about vantages is dangerous because you can find many personal opinions. See this question and its answers. I think some answers from there can also help you with your choice.

To finish, here are some links with tutorials about Retrofit and how to create your requests with this powerful library.

Comments