What is Retrofit?
Retrofit is a REST Client for Android and Java by Square. It makes it basically simple
to bring and transfer JSON by means of a REST based web benefit. In Retrofit you build
which converter is utilized for the information serialization. Regularly for JSON you utilize
GSon, yet you can add custom supporter to process XML or different conventions. Retrofit
utilizes the OkHttp reference for HTTP requests.
to bring and transfer JSON by means of a REST based web benefit. In Retrofit you build
which converter is utilized for the information serialization. Regularly for JSON you utilize
GSon, yet you can add custom supporter to process XML or different conventions. Retrofit
utilizes the OkHttp reference for HTTP requests.
Uses of Retrofit
To work with Retrofit you need mostly three classes.
- Model class is used to graph the JSON data to
- Interfaces which defines the available HTTP operations
- describing the URL endpoint for the HTTP affair.
- Retrofit.Builder class - Instance used for interface and the API which allow
Every method of an interface performs one possible API call. It must have a HTTP glossary
(GET, POST, etc.) to specify the request type and the relative URL. The return value stole
the response in a Call object with the type of the normal result.
(GET, POST, etc.) to specify the request type and the relative URL. The return value stole
the response in a Call object with the type of the normal result.
Retrofit converters and adapters
Retrofit Converters
Retrofit can be configured to use a specific converter. This converter handles the data
serialization. We can also create custom converters to process different protocols by
subclassing the Converter Factory class.
serialization. We can also create custom converters to process different protocols by
subclassing the Converter Factory class.
Retrofit Adapters
Retrofit can also be continued by adapters to get involved with other study like RxJava 2.x,
Java 8 and Guava.
Java 8 and Guava.
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.
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 possibility
would be to use an OkHttp interceptor for that.
manipulate the header for the request with the help of annotations. Another possibility
would be to use an OkHttp interceptor for that.
Authentication with annotations
Assume, that you want to request your user details, which requires you to authenticate.
You can do this by adding a new parameter to your API definition, like the following:
You can do this by adding a new parameter to your API definition, like the following:
Authentication with OkHttp interceptors
The above method only adds the credentials, if you request your user details. If you
have more calls that require you to authenticate, you can use an interceptor for this. An i
nterceptor is used to modify each request before it is performed and alters the
request header.
have more calls that require you to authenticate, you can use an interceptor for this. An i
nterceptor is used to modify each request before it is performed and alters the
request header.
Retrofit is mostly used in Android to learn more about Android check other
articles or go for Android Training in Chandigarh .
articles or go for Android Training in Chandigarh .
0 comments:
Post a Comment