Retrofit with RX runs OutOfMemory while trying to send a large BASE64

北城余情 提交于 2019-12-11 15:30:25

问题


I want to send a model throw Retrofit, which contains some standard fields and files, encoded in BASE64 format. I know, that this is a crazy way to send files, but I have a server with no backend developer.

So, when I'm sending, for example, 3 pdf files 20mb each, I runs out of memory which exception like this

Failed to allocate a 30544558 byte allocation with 2085152 free bytes and 26MB until OOM

I figured out, that it's a problem in parse of base64 string, which I'm already have in memory to Json throw GSON. I tried to create to create a custom adapter, according to Retrofit OutOfMemory exception while loading a files in BASE64 to server but runs out of ideas in case of a large model. What will fix this problem with passing of a large string to a JSON? Maybe I can use other adapters, not a GSON?

Model of documents field in Json and whole model

data class Document(
    var base64: MutableList<String?>,
    var mime: String?,
    var name: String?
)

data class OrderMainModel(
    var INN: String?,
    var KPP: String?,
    var addressOfEstateObject: String?,
    var addressOfGardenObject: String?,
    var agentFathersName: String?,
    var agentFirstName: String?,
    var agentLastName: String?,
    var approximateSizeOfEstateObject: Double?,
    var approximateSizeOfOKS: Double?,
    var area: String?,
    var bankAccountNumber: Int?,
    var companyAddress: String?,
    var companyName: String?,
    var documents: List<Document?>
}

Retrofit method

@Streaming
@POST("/api/order/")
fun makeOrder(@Header("Authorization") token: String, @Body order: OrderMainModel): Single<Response<PhoneNumberResponse>>

回答1:


Maybe, you should use Flowable with proper backpressure strategy instead of Single. But if you think it's for your adapter you can use Jackson because it's faster than Gson(According to this article). And if you want more performance maybe you should use Protobuf instead of JSON.



来源:https://stackoverflow.com/questions/58035503/retrofit-with-rx-runs-outofmemory-while-trying-to-send-a-large-base64

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!