Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

163
Visualizações
Handle empty body response from service

Im trying to call a service via POST that sends an e-mail to the user, the body always return empty, I have to deal with the response code. e.g. 204 = success.

Im trying to deal this way, but im not succeeding

Service:

@POST("xxxxxx")
fun resendBankSlip(@Path("userId") userId: Int): Deferred<Response>

ViewModel:

scope.launch {
        try {
            _loading.value = true
            val response = repository.sendEmail(userId)
            if (!response.isSuccessful) {
                _error.value = R.string.generic_error_message
            }
        } catch (e: Throwable) {
            _error.value = R.string.generic_error_message
        } finally {
            _loading.value = false
        }
    }

The error happens on val response = repository.sendEmail(userId)

Exception:

java.lang.IllegalArgumentException: 'okhttp3.Response' is not a valid response body type. Did you mean ResponseBody?
for method EmailService.sendEmail

Any idea?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You probably confused okhttp3.Response with retrofit.Response. Try to use retrofit2.Response wrapper in API response like that:

@POST("xxxxxx")
fun resendBankSlip(@Path("userId") userId: Int): Deferred<retrofit2.Response<Unit>>

After that you can easily get response code via response.code().

Also note that I passed Unit as Response's type argument because you don't need the body. In other cases you should pass an actual type of response body.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda