Retrofit Tutorial Part 4 – PUT, PATCH & DELETE REQUEST – Android Studio Tutorial



In part 4 of the Retrofit tutorial, we will learn how to make PUT, PATCH and DELETE requests on a REST API.
PUT and PATCH are the HTTP methods that we use to update data on a server.
While PATCH only changes the specific fields we send over, PUT completely replaces the existing entity with the one in the request body (or alternatively creates it).
A DELETE request deletes an existing resource.
PUT, PATCH and DELETE are usually applied on a single item rather than a collection as a whole.
As usual, we declare these methods in our API Java interface and annotate them with their corresponding HTTP call: @PUT, @PATCH or @DELETE. With @Path placeholders we dynamically specify the URL endpoint on which we want to modify a resource. PUT and PATCH both send a @Body annotated request body, which the GSON converter serializes into the JSON format. By calling serializeNulls on a GsonBuilder and passing the Gson instance to the GsonConverterFactory’s create method in addConverterFactory, we can tell GSON to not drop null values in the serialzation process. Alternatively we can send single fields @FormUrlEncoded.
DELETE returns a Call of type Void, with which we specify that we don’t want to read the response body. A successful delete operation is indicated by a 200 (OK) response code.
As usual, we run our network requests on a background thread, by calling enqueue instead of execute, and handle the results in the onResponse and onFailure callbacks.

Watch the whole playlist:

Example code for this part:
https://gist.github.com/codinginflow/27023cb7b764875c5dcf08636619eb85

____________________
💻 Find the BEST programming tutorials on TutHub:
https://tuthub.io

⭐ Get my MVVM Caching Course now:
https://codinginflow.com/caching

❗ Subscribe to the channel:
https://www.youtube.com/c/codinginflo…

📨 Subscribe to the Coding in Flow newsletter:
https://codinginflow.com/newsletter

❓ Join our free developer community:
https://discord.gg/TSnMvmc

📣 Follow Coding in Flow on other sites:
Facebook: https://www.facebook.com/codinginflow
Instagram: https://www.instagram.com/codinginflow
TikTok: https://www.tiktok.com/@codinginflow
Twitter: https://twitter.com/codinginflow
Github: https://github.com/codinginflow

💰 Business requests, sponsoring, etc.: info@codinginflow.com

Views :53186
android studio

Source by Coding in Flow

Mourad ELGORMA

Fondateur de summarynetworks, passionné des nouvelles technologies et des métiers de Réseautique , Master en réseaux et système de télécommunications. ,j’ai affaire à Pascal, Delphi, Java, MATLAB, php …Connaissance du protocole TCP / IP, des applications Ethernet, des WLAN …Planification, installation et dépannage de problèmes de réseau informatique……Installez, configurez et dépannez les périphériques Cisco IOS. Surveillez les performances du réseau et isolez les défaillances du réseau. VLANs, protocoles de routage (RIPv2, EIGRP, OSPF.)…..Manipuler des systèmes embarqués (matériel et logiciel ex: Beaglebone Black)…Linux (Ubuntu, kali, serveur Mandriva Fedora, …). Microsoft (Windows, Windows Server 2003). ……Paquet tracer, GNS3, VMware Workstation, Virtual Box, Filezilla (client / serveur), EasyPhp, serveur Wamp,Le système de gestion WORDPRESS………Installation des caméras de surveillance ( technologie hikvision DVR………..). ,

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *