Skip to content

Instantly share code, notes, and snippets.

@Barros9
Created May 26, 2020 18:11
Show Gist options
  • Save Barros9/e97cd73b336b51f0154f222f0795c7d6 to your computer and use it in GitHub Desktop.
Save Barros9/e97cd73b336b51f0154f222f0795c7d6 to your computer and use it in GitHub Desktop.
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.scalars.ScalarsConverterFactory
import retrofit2.http.GET
private const val BASE_URL = "PUT_HERE_URL"
private val retrofit = Retrofit.Builder()
.addConverterFactory(ScalarsConverterFactory.create())
.baseUrl(BASE_URL)
.build()
interface ApiService {
@GET("PUT_HERE_GET_VALUE")
fun get():
Call<String>
}
object Api {
val retrofitService: ApiService by lazy { retrofit.create(ApiService::class.java) }
}
@Barros9
Copy link
Author

Barros9 commented May 29, 2020

Retrofit Sample

Created a retrofit object pointing to the desired BASE_URL. An interface that expose the get() method, where @get annotation indicates the endpoint will be requested with the GET HTTP method, it returns a retrofit callback with a String.

Usage

Api.retrofitService.get().enqueue( object: Callback<String> {
    override fun onResponse(call: Call<String>, response: Response<String>) {
        // Success
    }
    override fun onFailure(call: Call<String>, t: Throwable) {
        // Failure
    }
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment