Retrofit - a library for sharing with the Internet (Part 1)

Content



  1. Retrofit API Description
  2. HTTP method
  3. Retrofit Connection


Retrofit API Description



According to the description on the library developer’s website, this is a typed http client for Android. Official documentation is available here . For ease of perception, some concepts must be described.



  1. How is network data exchanged?
  2. What are REST web servers
  3. Description of Retrofit
  4. Http methods


  1. All devices on the network exchange data with each other through the exchange protocols (TCP / IP, Http, Https, Ftp, etc.). A protocol is generally agreed rules for packaging, addressing, sending and receiving data. Using these protocols, the client and server (Web server) exchange data with each other
  2. Rest is a web server architecture that is used to exchange data on http and https protocols. Rest is a set of rules for executing HTTP methods.
  3. Retrofit is a java API that simplifies communication with rest web servers. Allows you to execute the HTTP method directly in the java language
  4. Http methods are standardized formats of procedures that the client asks the web server to execute. The http methods: Get, Head, Post, Put, Delete , Trace, Options, Connect and Patch. Retrofit only supports the first 5


HTTP method



1. Get and Head methods are considered the safest methods since receive data without changing it anywhere. The first method gets the title and body of the response, while the second only the title. Responses from the server come in the form of collections of values. Upon request, we can specify additional parameters for selection

For example, in the query someadress.ru?list=10&name=ivanov, we pass the selection parameters list = 10 and name = ivanov



2. Delete - the method performs the function of the same name, deletes entries on the server. Method does not require request body



3. Post and Put methods create or modify data on the server. Unlike other of these methods, the presence of the request body is mandatory. The Put method will create a record on the server if there is none, if there is a record then it will change it. Post creates entries and runs procedures on the server. You can remember the following expressions: Put - sends data and Post - Sends a message to the server



Examples are given in the Android Studio IDE



It is necessary to configure the exchange in such a way that the exchange between the client and the server takes place in one format. Mostly 3 data formats are used. Below is an example of displaying in three formats:



Json



{ "id":"1234", "name":"Vlad" }
      
      





XML



 <root> <id>1234</id> <name>Vlad</name> </root>
      
      





Protocol buffer



 { id:"1234", name:"Vlad" }
      
      





It is a pity that there is no format for the java class in this list, something like this:



 public class root{ private string id; private string name; … }
      
      





To transfer java objects to a server that can work in any language, retrofit uses a data converter. These converters are also called data serialization libraries. Retrofit includes 6 converters that use the most popular serialization libraries: Gson, Jackson, Moshi, Protobuf, Wire and Simple XML. Which of the converters to use depends on what format the server accepts.

For examples I use GSon converter since it is easy to use



Retrofit Connection



To demonstrate the capabilities of retrofit, we will create a demo application, version of Android 7.0 (Nougat). We go to the Download section on the official retrofit website and copy the line for grade in my case.



 implementation 'com.squareup.retrofit2:retrofit:2.6.1' <source/> ,      Gson.     retrofit        .       build    retrofit  . <source lang="kotlin"> implementation 'com.squareup.retrofit2:converter-gson:2.6.1' <source/>   ( Sync Now -).        retrofit.
      
      






All Articles