send get request to with kotlin and retrofit


I want to get altitude from api service and:

base url is

end point is api/v1/lookup?

and get two args lat and long like this,35

output is a json

How can i send get request and get data and please help


Hm. unfortunately you can’t send a request with varargs through retrofit. But, you can do it like this.

Do something like this..

lifecycleScope.launch {
     val latLong = arrayOf(41.161758, -8.583933)
     val result = withContext(Dispatchers.IO) {

     if (result.isSuccessful && result.code() == 200) {
         // If success do your sutff here
  • RetroClient
object RetroClient {

    private val httpInterceptor by lazy {

    private val client by lazy {
            .callTimeout(30, TimeUnit.SECONDS)
            .readTimeout(30, TimeUnit.SECONDS)
            .connectTimeout(30, TimeUnit.SECONDS)

    private val gson by lazy {

    fun getInstance(): RetroInterface {
        return Retrofit.Builder()
  • RetroInterface
interface RetroInterface {
    fun lookup(
        @Query("locations") locations: String,
    ): Call<LocationsModel?>
  • LocationsModel
data class LocationsModel(
    var results: List<Result>?

data class Result(
    var elevation: Int?,
    var latitude: Double?,
    var longitude: Double?
I/okhttp.OkHttpClient: <-- 200 OK (1411ms)
I/okhttp.OkHttpClient: Server: nginx/1.21.1
I/okhttp.OkHttpClient: Date: Sat, 27 Aug 2022 18:33:55 GMT
I/okhttp.OkHttpClient: Content-Type: application/json
I/okhttp.OkHttpClient: Transfer-Encoding: chunked
I/okhttp.OkHttpClient: Connection: keep-alive
I/okhttp.OkHttpClient: Access-Control-Allow-Origin: *
I/okhttp.OkHttpClient: Access-Control-Allow-Methods: PUT, GET, POST, DELETE, OPTIONS
I/okhttp.OkHttpClient: Access-Control-Allow-Headers: Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token
I/okhttp.OkHttpClient: Strict-Transport-Security: max-age=31536000; includeSubDomains
I/okhttp.OkHttpClient: {"results": [{"latitude": 41.161758, "longitude": -8.583933, "elevation": 117}]}
I/okhttp.OkHttpClient: <-- END HTTP (80-byte body)

Hope you understand.Thank you.

Answered By – Anubis94

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published