HTTP Request in Kotlin

后端 未结 11 2492
不知归路
不知归路 2020-11-28 06:59

I\'m completely new to Kotlin. I want to do a login validation using POST method and to get some information using GET method. I\'ve URL, server Username and Password alread

11条回答
  •  情话喂你
    2020-11-28 07:40

    You can use kohttp library. It is a Kotlin DSL HTTP client. It supports the features of square.okhttp and provides a clear DSL for them. KoHttp async calls are powered by coroutines.

    httpGet extension function

    val response: Response = "https://google.com/search?q=iphone".httpGet()
    

    you can also use async call with coroutines

    val response: Deferred = "https://google.com/search?q=iphone".asyncHttpGet()
    

    or DSL function for more complex requests

    val response: Response = httpGet {
        host = "google.com"
        path = "/search"
        param {
           "q" to "iphone"
           "safe" to "off"
       }
    }
    

    You can find more details in docs

    To get it with gradle use

    implementation 'io.github.rybalkinsd:kohttp:0.12.0'
    

提交回复
热议问题