How to use retrofit without any pojo or data class or model class

Viewed 1706

I am working on an application where I want to use retrofit, but response from API is very large and can not be converted in any data class or POJO class, and also the response is dynamic it increase with user actions for backup, So I want to ask this a long time that is there any way where I can use retrofit without making response data class or POJO class otherwise I have to move back to basic Http way of using REST api's .

If anyone have achieved this or used before , please give some idea how to achieve this, would be a great help. Thanks in advance.

2 Answers

From retrofit docs:

[1] Retrofit is the class through which your API interfaces are turned into callable objects.

[2] Retrofit turns your HTTP API into a Java interface.

Sole purpose of Retrofit is to abstract your API calls as Java interfaces. IT was meant to be used with interfaces and POJOs, it is designed that way. If you don't want to use POJOs, you can use OkHttp which is actually used by Retrofit under the hood. Retrofit should only be used when you need an abstraction for your HTTP calls as Java objects.

You can always just send strings via the @Body annotation.

public interface YourService{
    @POST("some/extension")
    Call<Object> makeCall(@Body String body);
}

You can access the body of the response like this:

service.makeCall(yourCustomString).enqueue(new Callback<String>() {
    @Override
    public void onResponse(Response<String> response) {
        String content = response.body(); // this gives the response body as a string
    }

    @Override
    public void onFailure(Throwable t) {...}
});

I still think using JSON-Converters is the way to go. You might just need a lot of nested classes inside of the wrapping response/request classes depending on the JSON structure. The size beeing different doesn't matter and can easly be created using lists and optional attributes. How big do your responses get? Moshi for example doesn't really have a size limit.

Related