Home > OS >  send get request to open_elevation.com with kotlin and retrofit
send get request to open_elevation.com with kotlin and retrofit

Time:08-30

I want to get altitude from api service and:

base url is https://api.open-elevation.com/

end point is api/v1/lookup?

and get two args lat and long like this

https://api.open-elevation.com/api/v1/lookup?locations=42,35

output is a json

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

CodePudding user response:

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) {
          RetroClient.getInstance().lookup(latLong.joinToString())
     }.awaitResponse()

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

    private val httpInterceptor by lazy {
        HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
    }

    private val client by lazy {
        OkHttpClient.Builder()
            .addInterceptor(httpInterceptor)
            .callTimeout(30, TimeUnit.SECONDS)
            .readTimeout(30, TimeUnit.SECONDS)
            .connectTimeout(30, TimeUnit.SECONDS)
            .build()
    }

    private val gson by lazy {
        GsonBuilder().setLenient().create()
    }

    fun getInstance(): RetroInterface {
        return Retrofit.Builder()
            .baseUrl("https://api.open-elevation.com/api/v1/")
            .client(client)
            .addConverterFactory(GsonConverterFactory.create(gson)).build()
            .create(RetroInterface::class.java)
    }
}
  • RetroInterface
interface RetroInterface {
    @GET("lookup")
    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?
)
  • RESULTS
I/okhttp.OkHttpClient: <-- 200 OK https://api.open-elevation.com/api/v1/lookup?locations=41.161758, -8.583933 (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.

  • Related