Friday 27 April 2018

What is Retrofit - Android training

What is Retrofit ?

Retrofit is a REST applicant for Android and Java by equal. It makes it approximately easy to fetch and upload JSON via a REST authorize web service. In Retrofit you configure which converter is used for the data fertilization. Typically for JSON handle GSon, but you can add custom teacher  to process XML or other protocols. Retrofit uses the OkHttp library for HTTP requests.Learn Retrofit with Android Training in Chandigarh.

Android Training in Chandigarh

 Using Retrofit

To work with Retrofit you need essentially three classes.
  • Model class which is used to design the JSON data to
  • Interfaces which defines the available HTTP operations
  • Retrofit.Builder class - detail which uses the interface and the Builder API which allows decide the URL endpoint for the HTTP actions.

Retrofit Converters


Retrofit can be construct to use a particular converter. This converter handles the data (de)serialization. Several converters are earlier available for different serialization formats.

To follow 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
  • Besides the recorded converters, you can also create custom converters to process other protocols by subclassing the Converter.Factory class.

Retrofit Adapters


Retrofit can also be lengthy by adapters to get confusions with other libraries like RxJava 2.x, Java 8 and Guava.
An analysis for applicable adapters can be found on Github square/retrofit/retrofit-adapters/.
For example the RxJava 2.x adapter can be obtained by using Gradle:

Retrofit authentication

Retrofit supports you with API calls that need authentication. Authentication can be done by using a username and a password (Http Basic authentication) or an API token.
There are two ways, how you can handle the authentication. The first method would be to manipulate the header for the request with the help of annotations. Another action would be to use an OkHttp intercepts for that.
  •  Authentication with annotations
  •  Authentication with OkHttp interceptors

Get advance learning on using retrofit with kotlin practically with Android Training in Chandigarh.

0 comments:

Post a Comment