In the Retrofit getting started tutorial you've learned how to create an Android app that connects to the public GitHub API and accesses objects on the server. In following tutorials you've learned how to create new objects and update existing ones.
In this tutorial you'll learn how delete objects on the server. This is usually done with DELETE
HTTP requests. If you're not familiar with HTTP requests yet, we recommend our tutorial on network basics.
If you're ready to go, let's start!
Retrofit Series Overview
- Getting Started and Creating an Android Client
- Basics of API Description
- Creating a Sustainable Android Client
- URL Handling, Resolution and Parsing
- How to Change API Base Url at Runtime
- Multiple Server Environments (Develop, Staging, Production)
- Share OkHttp Client and Converters between Retrofit Instances
- Upgrade Guide from 1.9
- Beyond Android: Retrofit for Java Projects
- How to use OkHttp 3 with Retrofit 1
- Synchronous and Asynchronous Requests
- Send Objects in Request Body
- Add Custom Request Header
- Manage Request Headers in OkHttp Interceptor
- Dynamic Request Headers with @HeaderMap
- Multiple Query Parameters of Same Name
- Optional Query Parameters
- Send Data Form-Urlencoded
- Send Data Form-Urlencoded Using FieldMap
- How to Add Query Parameters to Every Request
- Add Multiple Query Parameter With QueryMap
- How to Use Dynamic Urls for Requests
- Constant, Default and Logic Values for POST and PUT Requests
- Cancel Requests
- Reuse and Analyze Requests
- Optional Path Parameters
- How to Send Plain Text Request Body
- Customize Network Timeouts
- How to Trust Unsafe SSL certificates (Self-signed, Expired)
- Dynamic Endpoint-Dependent Interceptor Actions
- How to Update Objects on the Server (PUT vs. PATCH)
- How to Delete Objects on the Server
- Introduction to (Multiple) Converters
- Adding & Customizing the Gson Converter
- Implementing Custom Converters
- How to Integrate XML Converter
- Access Mapped Objects and Raw Response Payload
- Supporting JSON and XML Responses Concurrently
- Handling of Empty Server Responses with Custom Converter
- Send JSON Requests and Receive XML Responses (or vice versa)
- Unwrapping Envelope Responses with Custom Converter
- Wrapping Requests in Envelope with Custom Converter
- Define a Custom Response Converter
Delete Objects via an API
If you've read the previous tutorials, you know how to access, create and change objects. For us app developers deleting objects is actually the simplest request!
Of course, this only works if the server supports the DELETE
operation. Before you start implement the content of this tutorial, make sure your API supports this operation!
Let's review how our Retrofit interface declaration looks in the update Gist objects tutorial:
public class Gist {
// all Gist properties
// ...
}
public interface GistService {
// access Gists
@GET("gists")
Call<List<Gist>> getGists();
// create new Gist
@POST("gists")
Call<ResponseBody> createGist(@Body Gist gist);
// update existing Gist
@PATCH("gists/{id}")
Call<ResponseBody> patchGist(@Path("id") String id, @Body Gist gist);
@PUT("gists/{id}")
Call<ResponseBody> replaceGist(@Path("id") String id, @Body Gist gist);
// delete Gist
// ... coming up!
}
DELETE
HTTP Request
Before you can implement the interface declaration, you've to learn how the request itself looks. Because you don't need to send a payload, and are simply sending a command the request is simple:
DELETE /gists/12343
{}
In summary, all you need is the object ID to specify which object you want. The request doesn't have a payload, and you need to use the DELETE
HTTP method.
DELETE
Implementation in Retrofit
Let's implement this in Retrofit:
public interface GistService {
// ...
// delete Gist
@DELETE("gists/{id}")
Call<ResponseBody> deleteGist(@Path("id") String id);
}
And that's it! All you really need is the object ID and you're ready to delete whatever you don't like anymore (within your app)!
A last word of warning: make sure deleting an object on the server doesn't have negative side effects in your Android app. For example, if your view of the object is still open, you need to return to a safe state. If you cache objects, you need to remove the deleted object from the cache.
Summary
In this tutorial you've learned how to delete objects on the server. Deleting objects is quite easy with the DELETE
HTTP method. Retrofit conveniently offers this as a single line to us developers. Awesome!
Do you have further questions on this topic or about Retrofit in general? Just let us know on Twitter @futurestud_io or leave a comment below.
Enjoy coding & make it rock!