Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom converter for Retrofit 2

I have to handle a dynamic JSON responses.

Before, I was using classes and annotations as follows:

public class ChatResponse {      @SerializedName("status")     private int status;      @SerializedName("error")     private String error;      @SerializedName("response")     private Talk response;      public int getStatus() {         return status;     }      public String getError() {         return error;     }      public Talk getResponse() {         return response;     } } 

When the status is 1 (success) the onResponse is fired and I can get a ChatResponse object. But, when the status is 0, the response is false in the JSON representation and it fails (onFailure is fired).

I want to create my custom converter, and this question has a good example, but that example is for Retrofit 1.

I have to create a class that extends Converter.Factory, but I don't know how to override the methods of this class.

Actually I have the next:

@Override public Converter<ResponseBody, ?> fromResponseBody(Type type, Annotation[] annotations) {      return super.fromResponseBody(type, annotations); }  @Override public Converter<?, RequestBody> toRequestBody(Type type, Annotation[] annotations) {      return super.toRequestBody(type, annotations); } 

How I can parse the JSON response by my own at this point?

Thanks in advance.

like image 273
JCarlosR Avatar asked Feb 19 '16 09:02

JCarlosR


People also ask

What is Retrofit converter?

Retrofit is a very popular HTTP client library for Android. Using Retrofit makes it easy to parse API response and use it in your application. It has built-in GSON converter that can automatically parse HTTP response into an Object or any other types in Java that can be used in your code.

What is GSON converter factory?

A converter which uses Gson for JSON. Because Gson is so flexible in the types it supports, this converter assumes that it can handle all types. If you are mixing JSON serialization with something else (such as protocol buffers), you must add this instance last to allow the other converters a chance to see their types.

What is GSON converter in Android?

GitHub - google/gson: A Java serialization/deserialization library to convert Java Objects into JSON and back. Product. Actions.

Which method is used to add the converter to retrofit instance?

Call the created instance of that class (done via create() ) to your Retrofit instance with the method addConverterFactory() . After you've added the converter to Retrofit, it'll automatically take care of mapping the JSON data to your Java objects, as you've seen in the previous blog post.


1 Answers

I was looking for a simple example about how to implement a custom converter for Retrofit 2. Unfortunately found none.

I found this example but, at least for me, it's too complicated for my purpose.

Happilly, I found a solution.

This solution is to use GSON deserializers.

We don't need to create a custom converter, we just have to customize the GSON converter.

Here is a great tutorial. And here is the code I used to parse the JSON described in my question:

  • Login Deserializer: Defines how to parse the JSON as an object of our target class (using conditionals and whatever we need).

  • Custom GSON converter: Builds a GSON converter that uses our custom deserializer.

like image 184
JCarlosR Avatar answered Sep 19 '22 10:09

JCarlosR