简体   繁体   English

如何在没有任何 pojo 或数据的情况下使用 retrofit class 或 model

[英]How to use retrofit without any pojo or data class or model class

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. 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.提前致谢。

From retrofit docs:来自 retrofit 文档:

[1] Retrofit is the class through which your API interfaces are turned into callable objects. [1] Retrofit 是 Retrofit 是 API 接口转换为可调用对象的 class。

[2] Retrofit turns your HTTP API into a Java interface. [2] Retrofit 将您的 HTTP API 变成 ZD52387880E1EA29817A72D37 接口。

Sole purpose of Retrofit is to abstract your API calls as Java interfaces. Retrofit 的唯一目的是将您的 API 调用抽象为 Java 接口。 IT was meant to be used with interfaces and POJOs, it is designed that way. IT 旨在与接口和 POJO 一起使用,它就是这样设计的。 If you don't want to use POJOs, you can use OkHttp which is actually used by Retrofit under the hood.如果你不想使用 POJO,你可以使用OkHttp ,它实际上是由 Retrofit 在引擎盖下使用的。 Retrofit should only be used when you need an abstraction for your HTTP calls as Java objects. Retrofit 仅应在您需要将 HTTP 调用抽象为 Java 对象时使用。

You can always just send strings via the @Body annotation.您始终可以通过 @Body 注释发送字符串。

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.我仍然认为使用 JSON-Converters 是 go 的方法。 You might just need a lot of nested classes inside of the wrapping response/request classes depending on the JSON structure.根据 JSON 结构,您可能只需要包装响应/请求类中的许多嵌套类。 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.例如,Moshi 并没有真正的大小限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM