Blog post

09 August, 2020

WHAT IS RETROFIT??

Educational blog

...

Retrofit is a REST Client for Java and Android. REST Client is a method or a tool to invoke a REST service API that is exposed for communication by any system or service provider. For example: if an API is exposed to get real-time traffic information about a route from Google, the software/tool that invokes the Google traffic API is called the REST client. 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 we basically need the following three classes:

 

1> MODEL:-

 

* This class is used as a JSON Model which represents the data we are dealing with.

 

2> INTERFACE:-

 

* The interface defines the possible HTTP operations.(GET,POST etc)

 

* The endpoints are defined inside of an interface using special retrofit annotations to encode details about the parameters and request method.

 

3> RETROFIT.BUILDER CLASS: -

 

*In this class, we specify the base URL for the service. An instance that uses the interface and the builder API to allow defining the URL endpoint for HTTP operations.

 

* We create the retrofit instance using the Retrofit Builder class and specify a factory(GsonConverterFactory) for deserializing the response using the Gson library.

 

OKHTTP CLIENT INTERCEPTOR WITH RETROFIT:-

 

-> Ok, Http Client Interceptors intercept, change or modify requests and responses, call adapter factory for supporting service method return types other than the usual Call as well as converter factory for serialization and deserialization of an object using Gson.

 

DEFINING ENDPOINTS AT INTERFACES

 

-> The return value is always a parameterized Call<T> object such as Call<User>. If we do not need any type-specific response, we can specify return value as simply Call<ResponseBody>. An endpoint specifies an annotation of the HTTP method (GET, POST, DELETE, PATCH, etc.) and the method that will be used to dispatch the network call.

 

RETROFIT CONVERTERS

 

->Gson the library is extensively used to serialize and deserialize JSON data. Retrofit 2 now supports many different parsers for processing network response data along with Gson, like Moshi, Protobuf,

Wire, simple XML.

 

RETROFIT REQUEST CALLBACK

 

The Call class is the starting point for every network request with Retrofit. Usually, we want to execute the request asynchronously with the en queue method. The method expects a typed implementation of the Retrofits Callback class, which expects the implementation of two methods (on response and on Failure).

 

RETROFIT ADAPTERS

 

->Retrofit is extended by adapters to get involved with other libraries like RxJava 2.x, Java 8 and Guava.

 

RX JAVA

 

=> RxJava is a Java VM implementation of ReactiveX a library for composing asynchronous and event-based programs by using observable sequences.

 

=>The building blocks of RxJava are Observables and Subscribers. Observable is used for emitting items and Subscriber is used for consuming those items.

 

=>Observables often don't start emitting items until someone subscribes to them. This is a great feature because this way we should save our performances.

 

=>Subscriber subscribes to Observable, then Observable calls Subscriber.onNext() for any number of items, if something goes wrong here is Subscriber.onError() and if all finishes fine, here is Subscriber.onCompleted().

 

This is the basic structure of retrofit. Can refer to the links below to acquire knowledge of Adding headers, Form data, Multipart forms, Form URL encoding, Posting JSON data, Using Authentication Headers, etc.

 

 


Story by JTPC Admin
You may also like