In this example, we are using fluent API with Retrofit.Builder and OkHttpClient to create Retrofit instance. Now it is time to use create Retrofit REST client and make actual API call. In method parameter, we can pass path and query parameters, and request body as well in PUT/POST requests.In method declaration, we must return the data that is expected from the server wrapped it into a typed Retrofit Call class.We can use the appropriate Retrofit annotations for each HTTP method: or In the method annotation, we should specify the relative endpoint URL of REST resource.Public interface UserService Call long id) Now, we can use the Retrofit annotations to create the service interface with required mapping information and request/response classes. If only populated those fields which names match the response fields. The converter, Gson, does not complain in case of there is a mismatch in number of fields. Let’s assume we are only interested in consuming id, name and email fields and we do not want to consume avatar field. We are taking example of one such API available to use for free. The next step to use Retrofit is to model the REST API, which we want to consume in application.
#Bordertool 2 tutorial android#
In Android application, enable Internet permission as well because Retrofit performs HTTP requests against an API running on a server somewhere in the Internet. In this tutorial, we will be sending and receiving data in JSON format, so we have added converter-gson dependency as well.Ĭompile '2:retrofit:2.6.1'Ĭompile '2:converter-gson:2.6.1' To include Retrofit 2 in our project, we shall include following dependencies in build file. It allows to make a synchronous or asynchronous HTTP request to the remote webserver.
![bordertool 2 tutorial bordertool 2 tutorial](https://manlasopa169.weebly.com/uploads/1/2/6/4/126495376/240680304.jpg)
The entire network call + JSON/XML parsing is completely handled by Retrofit (e.g. It essentially lets us treat API calls as simple Java method calls, so we only define which URLs to hit and the types of the request/response parameters as Java classes. Retrofit turns your HTTP API into a Java interface.
#Bordertool 2 tutorial for android#
Retrofit developed by square and in documentation, it is type-safe REST client for Android and Java. In this Retrofit 2 tutorial, we will learn the basics of Retrofit and then we will create an Android client for HTTP requests against a REST API.