What Is Retrofit?
A Retrofit is special HTTP client for Android and Java. The type-safe client connects to a REST web service by translating the API into Java interfaces. This powerful library makes it easy to consume JSON or XML data, parsed into Plain Old Java Objects. The open-source Retrofit has its foundation on top of some other powerful libraries and tools. It makes use of OkHttp to handle network requests. As you have read about multiple tools and tips in the Software world through these blogs, here are some more freelance tips.
There are three main classes to work with Retrofit:
- Model class which is used to map the JSON data to
- Interfaces which defines the possible HTTP operations
- Builder class – Instance which uses the interface and the Builder API which allows defining the URL end point for the HTTP operation.
Why Use Retrofit?
Why develop your type-safe HTTP library when one already exists? Creating library includes making connections, caching, retrying failed requests, threading, response parsing, error handling, and much more. What is available is a well-planned, documented and tested library. It is a great source of readymade code for freelance web developers.
While using Retrofit, for performing network operations, you need to include the INTERNET permission in the application manifest, which is AndroidManifest.xml.
You may have used Volley as networking library. Retrofit is an alternative you can consider in place of volley regarding ease of use, performance and many other things. It is known as REST client for Android, which is built by Square. This tool is useful for Android developers, and they can make all network stuff much easier.
Both Retrofit and Volley are easy to use libraries and provides the feature to make network calls both synchronously and asynchronously. For request and response handling, Retrofit treats the API calls as simple Java method calls, and it also handles the Json/Xml parsing.
Generating and Importing Models
You can create models automatically from the JSON response data by leveraging a very useful tool: jsonschema2pojo. To import Data Models to Android Studio, you can go back to the Android Studio.
You can create a new sub-package inside the main package and name it data. You can create another package within this package and can call it as a Model. Within this package, you can create a new Java class and name it Post. After this preparation, you can copy the Post class that was generated by jsonschema2pojo and paste it inside the Post class you created.
How to create a Retrofit Instance
The retrofit instance can be created using the Retrofit Builder class and configure it with a base URL. You can create new sub-package and further packages inside this package to add Java Class, which is the Retrofit Client.
Creation of API Interface & Utilities
Now you need to create an APIService. It is the most important interface which contains the methods you will use to execute HTTP requests such as POST, PUT, and DELETE.
You can create a new utility class by creating a class in Data.Remote, which will have the base URL as a static variable and will also provide the API Service interface. For performing any synchronous request, you can use the execute() method in a Call instance. The synchronous methods on the main/UI thread can block any user action and better not to use them on UI. Instead, use them on a background thread.
Retrofit can be configured to use a specific converter. This converter handles the data (de)serialization. Specific converters are listed below:
To convert to and from JSON:
- Gson: com.squareup.retrofit:converter-gson
- Jackson: com.squareup.retrofit:converter-jackson
- Moshi: com.squareup.retrofit:converter-moshi
To convert to and from Protocol Buffers:
- Protobuf: com.squareup.retrofit:converter-protobuf
- Wire: com.squareup.retrofit:converter-wire
To convert to and from XML:
- Simple XML: com.squareup.retrofit:converter-simplexml
Retrofit 1 contains RxJava by default, but in Retrofit 2 you need to include extra dependencies. Retrofit comes with a default adapter for executing Call instances. You can change Retrofit’s execution mechanism to include RxJava by including the RxJava CallAdapter. The adapter is a common pattern which helps to bind view and data, so it is better to implement an adapter.
Retrofit can parse many types of Web API responses such as Boolean, Integer, String, Date, Object and Collections.
Advantages of Retrofit 2.0 Release
The main advantage of type-safe HTTP clients is that you only need to worry about the semantics of the queries that you send over the network. You don’t have to worry about the details of how to construct URLs, specify parameters correctly and such critical things. This library makes this easy, as you need to write only a couple of interfaces.
You can always enable JRebel for Android for all projects. It’s packaged as an Android Studio plugin, so enabling it means that when you click the custom button to run the app, everything else is taken care by JRebel for Android. It is a productivity tool for an Android developer that can instantly update code and resources on the running device or emulator. That essentially means that while you’re developing the application.
Retrofit 2 is a type-safe HTTP client for Android and Java, but it’s a library, so to use it, you need to declare the correct dependencies. That is easy, however, note that we need to depend explicitly on the JSON converter to transform the JSON responses to the model classes. It is different than Retrofit 1, so be careful. With more and more freelance work in this area, freelancers have an advantage over here. You can download the latest Retrofit version and start implementing in the new projects. Considerable online help is available for this client.
Retrofit is an awesome library to use in Mobile App development. You can learn more about Retrofit, do refer to the official documentation. There are multiple advantages as discussed above but if you are looking for the cache, Retrofit does not support caching. This client supports Request cancellation. You have direct code examples available on how to use Retrofit on the GitHub.