Unresolved reference : retrofit while creating retrofit instance

Viewed 820

I am working on an android app which request Mars photos ans use it to display it on screen. To make an request.And trying to use A public Api object that exposes the lazy-initialized Retrofit service. below is source code with error

import retrofit2.Retrofit
import retrofit2.converter.scalars.ScalarsConverterFactory
import retrofit2.http.GET

class MarsApiService {
    public val retrofit = Retrofit.Builder()
    .addConverterFactory(ScalarsConverterFactory.create())
    .baseUrl(Companion.BASE_URL)
    .build()
interface MarsApiService{
    @GET("photos")
    fun getPhotos(): String
}
object MarsApi {
    val retrofitService: MarsApiService by lazy { retrofit.create(MarsApiService::class.java) }
}



companion object {
    private const val BASE_URL = "https://android-kotlin-fun-mars-server.appspot.com"
}
}

17th line the code inside object MarsApi pops up errors Unresolved reference : retrofit. The call to create() function on a Retrofit object is expensive and the app needs only one instance of Retrofit API service. So, i exposed the service to the rest of the app using object declaration.

What I have tried:

The code is working if i bring code inside object MarsApi out but doing so may result in multiple instance of retrofit.

3 Answers

code1

interface MarsApiService {
    @GET("photos")
   suspend fun getPhotos(): String

    companion object {
        private const val BASE_URL = "https://android-kotlin-fun-mars-server.appspot.com"
        val marsApiService: MarsApiService by lazy {
            Retrofit.Builder()
                .addConverterFactory(ScalarsConverterFactory.create())
                .baseUrl(BASE_URL)
                .build().create(MarsApiService::class.java)
        }
    }
}

code2

private const val BASE_URL = "https://android-kotlin-fun-mars-server.appspot.com/"

private val retrofit = Retrofit.Builder()
    .addConverterFactory(ScalarsConverterFactory.create())
    .baseUrl(BASE_URL)
    .build()

interface MarsApiService {

    @GET("photos")
    suspend fun getPhotos(): String
}

object MarsApi {
    val marsApiService: MarsApiService by lazy { retrofit.create(MarsApiService::class.java) }
}

run


fun main() = runBlocking {
    val rs = marsApiService.getPhotos()
    println(rs)
}

In your code, retrofit by lazy reference not resolved cause of retrofit is property of class, move the retrofit globally like using (Companion) object or like code2 (highest hierarchy?)

Hope this link

make you clear the difference between object and companion object and when to use them. Happy learning

It was the same problem that I had. I solved it just excluding the class MarsApiService I didn´t use companion object too.

The right code is below

import retrofit2.Retrofit
import retrofit2.converter.scalars.ScalarsConverterFactory
import retrofit2.http.GET

private const val BASE_URL = "https://android-kotlin-fun-mars-server.appspot.com"

public val retrofit = Retrofit.Builder()
    .addConverterFactory(ScalarsConverterFactory.create())
    .baseUrl(Companion.BASE_URL)
    .build()

interface MarsApiService{
    @GET("photos")
    fun getPhotos(): String
}

object MarsApi {
    val retrofitService: MarsApiService by lazy { 
        retrofit.create(MarsApiService::class.java) 
    }
}

Related