0

I am changing the way our application works to use retrofit instead of just OkHTTP

The way it used to work is we would send the request, retrieve the body as an input stream and read all bytes into a string.

After that we would parse the body using gson.

The problem is that the server seems to have a problem with the configuration (which I am told is on the list of things to fix but will take a long time) so for example it may return 400 bytes of data, but will send the message that the bytes are actually 402.

The way we currently handle it is by catching the EOF exception and ignoring it, and then parsing the returned string normally.

right now I use the following request to get the entities I want

@GET("/services/v1/entities")
suspend fun getEntities() : List<ServerEntity>

which , when there is no error, works correctly

the solutions I've found so far are either

a) use the following code to retry all requests until I do not get an EOF exception:

internal suspend fun <T> tryTimes(times: Int = 3, func: suspend () -> T): T {
    var tries = times.coerceAtLeast(2)
    try {
        var lastException: EOFException? = null
        while (tries > 0) {
            try {
                return func.invoke()
            } catch (eof: EOFException) {
                lastException = eof
                tries--
            }
        }
        throw lastException!!
    } finally {
        log.d("DM", "tried request ${times.coerceAtLeast(2) - tries} times")
    }
}

which most of the time logs either 0 or 1 tries

or change all my requests to

@GET("/services/v1/entities")
suspend fun getEntities() : ResponseBody 

and parse the stream manually ( ResponseBody may be incorrect but you can understand what I mean)

is there a way to use my original function and make retrofit know that in the case of an EOF exception it should resume instead of stopping?

Cruces
  • 3,029
  • 1
  • 26
  • 55
  • try this https://stackoverflow.com/questions/33228126/how-can-i-handle-empty-response-body-with-retrofit-2 – Jawad Malik Oct 08 '19 at 10:06
  • yeah that's basically the second solution I found, the one with the responsebody, what I want to do is keep my original implementation (which returns List) and make retrofit just ignore EOF exceptions – Cruces Oct 08 '19 at 10:08
  • 1
    refer to this one https://stackoverflow.com/questions/42802951/retrofit2-handle-condition-when-status-code-200-but-json-structure-different-tha – Jawad Malik Oct 08 '19 at 10:14
  • 1
    I have to read this thoroughly, and I don't know if it solved this specific problem but going by the description it definitely solves a different problem I had, thanks for the assist – Cruces Oct 08 '19 at 10:26

0 Answers0