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

220
Visualizações
How to Asynchrounsly call HTTP Post API?

I have an issue with my android app. I'm fairly new with it and have some issues with finding the correct documentation for a asynchronous. I'm using the kohttp library to help me a bit.

The thing is, you can't run this on the main UI thread so I want to make this request Async. I can't find a clear reference in the documentation and I don't really know how to do this in plain Kotlin.

This is what I come up with; in a separate class named LoginCall. I tried other answers, this however didn't result in success. How can I run this on a new thread and still use the response?

    class LoginCall {
    fun callLoginRequest(a:String, b:String): Any {

        val response: Response = httpPost {
            host = "XXX"
            path = "XXX"

            param { }
            header { }

            body {
                form {
                    "email" to a
                    "password" to b
                }
            }
        }

        return response
    }
}
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

There are many ways to achieve this, if you're using android as the underlying platform, you can use the native component called AsyncTask a good SO post on how to use it.

If you wish to leverage kotlin as a language and the features provided by it, you can try using coroutines ref.

Personally, i would recommend coroutines, it simplifies exception and error handling, also prevents callback hell.

here's a sample of the same code in a coroutine,

// global 
private val mainScope = CoroutineScope(Dispatchers.MAIN + SupervisorJob())

// inside a method 
mainScope.launch{
  withContext(Dispatchers.IO){
    // do your async task here, as you can see, you're doing this in an IO thread scope. 
  }
}

over 4 years ago · Santiago Trujillo Relatório

0

Since kohttp 0.10.0 you can use async methods for such cases. You can try them.

Code example:

suspend fun callLoginRequest(a:String, b:String): Any {

    val response: Differed<Response> = httpPostAsync {
        host = "XXX"
        path = "XXX"

        param { }
        header { }

        body {
            form {
                "email" to a
                "password" to b
            }
        }
    }

    // for further usage in coroutines
    return response.await() 
}

And call this function from coroutine

over 4 years ago · Santiago Trujillo Relatório

0

Also, you can create an issue to implement asyncHttpPost.

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