What best practices when implementing requisitions?

10

When implementing calls to a webservice I have worked with the following schema: implement a class that extends AsynkTask and within method doInBackground() I make the call to my service. One particularity here is that instead of using the HttpUrlConnection class I have used a Spring framework , Spring for Android , which provides a number of features, including code cohesion.

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

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

    
asked by anonymous 22.08.2015 / 03:57

3 answers

10

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

// Annotation para especificar uma requisição ao webservice
@GET("/usuarios/{id}/compra")

// 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 request the webservice and send the response to callback , which returns the user's purchase.

    
22.08.2015 / 18:14
3

There are several ways to make requests to WebServices, through code that would become very extensive or through APIs, a very simplified way of making such requests is in these ways:

link

link

Remembering that there are methods using REST with PHP as well.

Note that there is a very good framework called "Spring", it is very dynamic and reliable! Check out the second link to use it. If you want to understand how a WebService works and how to implement it in a simplified way, see the second link. Hope this helps! Thank you.

    
25.08.2015 / 22:29
3

Android has now adopted the library for HTTP requests of java.net . The recommended libraries before this were from the apache package. Google along with AOSP are developing a library called Volley very light and powerful. I recommend you use the Volley which in the near future will be the default library for web request, but it is important to know the difference between them. In this link has a very interesting comparison about Volley and Retrofit (mentioned by regmoraes above). But nothing better than comparing libraries in specific cases.

See also how to add Vooley as a dependency in Gradle (android-volley) .

    
28.08.2015 / 20:20