java – What best practices when implementing requests?


When implementing calls to a webservice I've been working with the following scheme: I implement a class that extends AsynkTask and inside the doInBackground() method I make the call to my service. A peculiarity here is that instead of using the HttpUrlConnection class HttpUrlConnection have been using a Spring framework , Spring for Android , which provides a series of facilities, including code cohesion.

Although this scheme works perfectly, it is somewhat unproductive and even tiring. Because, for each request I need to implement a new AsyncTask .

Is there any good practice, even if unofficial, or any approach that the community has taken to facilitate the development of calls to a webservice and reduce the amount of code implemented for this purpose?


If you are using JSON, you can use Retrofit to make the requests. In Retrofit it is possible to make synchronous and asynchronous calls, in the case of asynchronous calls (outside the UIThread ), you would need to do something like this:

// Annotation para especificar uma requisição ao webservice

// Nome do método que fará a requisição
void getCompraUsuario(@Path("id") int id, Callback<Compra> callback);

In this case, the getCompraUsuario method will make the request to the webservice and will send the response to the callback , which returns the user's purchase.

Scroll to Top