How To Post JSON With Retrofit

This tutorial is going to show how to post JSON with Retrofit, a type-safe HTTP client for Android and Java.

1. Library Dependency

We’re going to use Retrofit 2.3.0 for all examples presented in this tutorial. And To use Retrofit with Maven, we will need to include the following dependency into our pom.xml file:

To use Retrofit with Gradle:

2. Rest API

In this tutorial, we will use Retrofit to post Json to a Rest API with the following information:

URL to create a resource (book)

Input

Name Type
name the name of the book
author the author of the book
Response
STATUS 201 if the book is created successfully.

3. Posting JSON With Retrofit

Basically, to post JSON with Retrofit, we will need the following steps:

  • To define a POJO class to represent the JSON that we want to post to the Rest API with Retrofit, or we can simply use a Map with its keys are the field names and values are the field values.
  • When we use the above POJO object as a service method parameter, we annotate it with a @Body annotation to imply that we want to directly control the request body of a POST/PUT request. The object will be serialized using the Retrofit instance Converter and the result will be set directly as the request body.

Next, we will get through the first way to post JSON with Retrofit: to define a POJO class.

3.1. Defining A POJO Class

3.1.1. POJO Class

Let’s define a POJO class called Book to represent the request body:

3.1.2. Defining A Service Interface

Next, we will need to define a service interface which contains all the methods we need to call/invoke from the Rest API:

Notice that we annotated the parameter with @Body annotation.

3.1.3. Posting The POJO Object.

Finally, let’s see how we post the Json with Retfofit:

3.2. Using A Map (HashMap) To Represent The JSON

Instead of defining a POJO class to represent the JSON, we can use a Map to represent the JSON we want to post with Retrofit, then use @Body annotation to instruct Retrofit to serialize the Map into JSON before sending to the Rest API.

3.2.1. Defining A Service Interface

Let’s define another method called updateBook in the above service interface as follows:

Notice that the parameter of the method is an java.util.Map.

3.2.2. Posting The Map

Let’s see how we create the Map and post it in the following example:

After creating the Retrofit and service interface objects, we create a HashMap to represent the Json and then post the map with Retrofit.

4. Conclusions

The tutorial has illustrated how to post JSON with Retrofit by either defining a POJO class or using a Map to represent the JSON, then annotate those with the @Body annotation which instructs Retrofit to serialize those into JSON before making a request to the Rest API. And we can see that defining a POJO class is suitable for all cases that we have fixed JSON schema while the Map is very flexible for the remaining cases.

The source code presented in the tutorial is available on my Github project. And it’s a Maven based project; it’s will be easy to import into IDE such as Eclipse, IntelliJ IDEA, etc.

Below are other related articles for your references:

How to Post with Retrofit 2

Java REST Client Example

Java REST Client Example With Retrofit 2

 

How to Post with Retrofit 2

This tutorial is going to cover about how to post with Retrofit 2, a type-safe HTTP client for Android and Java applications. We’re going to get though some popular use cases and examples where Retrofit 2 can be used such as how to post a JSON string, how to post an object, how to post a multipart form, encoded form and so on.

1. Preparation

We’re going to use Retrofit 2, version 2.1.0 for all Retrofit 2 post examples in the tutorial. And we also use the Gson library to serialize and deserialize JSON data. Therefore, the Maven dependencies we will be as the following:

The REST API we’re going to use for all Retrofit 2 post examples in this tutorial is httpbin, an free HTTP Request & Response Service for testing.

2. Post An Object

Let’s start to post with Retrofit 2 by an example which we’re going to post an object to the REST API.

Firstly, we need to define an endpoint inside of an interface using Retrofit 2 annotations which will define the HTTP method, request body, parameters and so on.

In the interface, the @POST annotation is used to define the HTTP POST method, and the @Body annotation is used to define a single request body. In addition, the return value is always a parameterized Call<T> object such as Call<Book> and the response will be converted into the T type, Book in this example. If we don’t need any type-specific response, we can specify return value as Call<ResponseBody>.

Next, let’s create the Retrofit 2 instance and test the method:

3. Post A JSON String

Let’s say we have a following JSON string and we want to use Retrofit 2 to post the string to the remote API:

To post a JSON string with Retrofit 2, we can define an POJO that can represent the JSON that we need. For instance, a POJO represents the JSON can be defined in the following way:

Then we can define the endpoint as below:

Because we annotate the parameter with the @Body annotation, Retrofit 2 will use the converter library to serialize the parameter into JSON.

Next, let post the JSON string like we post an object with Retrofit 2:

For more detail about posting JSON, visit another tutorial: How To Post JSON With Retrofit

4. Post A Form Encoded

Form-encoded data is sent when @FormUrlEncoded is present on the method. Each key-value pair is annotated with @Field containing the name and the object providing the value. Let’s see an example which we use Retrofit 2 to post a form encoded to update the resource from remote API.

Endpoint:

The @FormUrlEncoded annotation denotes that the request body will use form URL encoding. Fields should be declared as parameters and annotated with the @Field annotation.

Next, let’s define a test method:

4.1. Post A Form Encoded With @FieldMap

In situations where we just need to send some fields that should be updated for a given resource, we can use the Retrofit 2’s @FieldMap which is a named key/value pairs for a form-encoded request. We can pass a Map which its keys are the field names and paired values are the values we want to update for the resource.

Let’s the following example which we post a form encoded with @FieldMap to update a resource from the API.

The endpoint:

The test method:

5. Post A Multipart Form

We can use Retrofit 2 to post a multipart form in order to upload files or images to remote API. Multipart requests are used when @Multipart is present on the method. Parts are declared using the @Part annotation. Let’s see an example which we post a multipart form to add a cover image for a given Book identified by its id.

Endpoint:

Next, let’s define a test method:

6. Conclusion

The tutorial has just illustrated how to post with Retrofit 2 in different use cases and operations, from how to post an object, how to post a JSON string, how to post a form encoded to how to post a multipart form.

All the Retrofit 2 post examples code in the tutorial is available on my Github project. It’s an Maven based project, so it’s easy to import and run it in any IDE such as Eclipse, Intellij, etc.

Below are other related articles:

Java REST Client Example With Retrofit 2

Java REST Client Example