Skip to main content

Posts

Showing posts from 2018

Using Retrofit 2.x as REST client

This tutorial explains the usage of the Retrofit library as REST client. What is Retrofit? Retrofit is a REST Client for Android and Java by Square. It makes it relatively easy to retrieve and upload JSON (or other structured data) via a REST based webservice. Using Retrofit To work with Retrofit you need basically three classes. Model class which is used to map the JSON data to Interfaces which defines the possible HTTP operations Retrofit.Builder class - Instance which uses the interface and the Builder API which allows defining the URL end point for the HTTP operation. Every method of an interface represents one possible API call. It must have a HTTP annotation (GET, POST, etc.) to specify the request type and the relative URL. The return value wraps the response in a Call object with the type of the expected result. Add these two lines to the build.gradle file: compile 'com.squareup.retrofit2:retrofit:2.3.0' compile 'com.squareup.retrofit2:con