android-fragmentskotlinretrofit2enqueue

Kotlin Retrofit2 Enqueue "Unresolved reference Enqueue"


Hi I am getting an Unresolved reference Enqueue message and running out of options to resolve.

The Callback is made in a button setOnCLickListener in a Fragment.

Other suggestions have been to set the Gradle version to 3.1.3 (previously using 3.6.1) but the error persists.

        btnSearch.setOnClickListener {

            val service = RetrofitClientInstance.retrofitInstance?.create(GetFlightService::class.java)
            val call = service?.getFlightData(
                "QF",
                "642",
                "departing",
                "2020",
                "05",
                "09",
                "FS",
                "xxxxxxxxx",
                "xxxxxxxxxxxxxxxxxxxxxxxxxxx") //appID and appKey x'd out :)

            call?.enqueue(object : Callback<RetrievedFlightData>{
                override fun onResponse(
                    call: Call<RetrievedFlightData>,
                    response: Response<RetrievedFlightData>
                ) {
                    if (response.code() == 200) {

                        val flightDataBody = response.body()!!

                        val flightData: FlightDTO = getFlightDTOData(flightDataBody)

                    }
                }

                override fun onFailure(call: Call<RetrievedFlightData>, t: Throwable) {
                    TODO("Not yet implemented")
                    Toast.makeText(activity,"Error: Callback faliure - ${t.message}", Toast.LENGTH_LONG).show()

                }
            })

        }

Interface is:

interface GetFlightService {


    @GET ("/flex/schedules/rest/v1/json/flight/")
    fun getFlightData(@Query("carrierFSCode") carrierFSCode: String,
                      @Query("flightNumber")flightNumber:String,
                      @Query("departing") departing:String,
                      @Query("year") year:String,
                      @Query("month") month:String,
                      @Query("day") day:String,
                      @Query("codeType") codeType: String,
                      @Query("appID") appID: String,
                      @Query("appKey") appKey:String


    )

}

and ClientInstance

object RetrofitClientInstance {

    private var retrofit: Retrofit? = null

    private const val  BASE_URL = "https://api.flightstats.com"

    //create a retrofit instance, only if it has not been created yet.

    val retrofitInstance: Retrofit?
        get() {
            if (retrofit == null){
                retrofit = retrofit2.Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build()
            }

            return retrofit
        }
}

The Client instance works as I have made other calls that work OK.

Another issue is that I need to wait for the response before I proceed. I have previously used newSingleThreadExecutor on another call but I understand this is not best practice. Any help on this would also be appreciated.

thanks

joncb


Solution

  • Change your getFlightData() to return Call<RetrievedFlightData>. So it becomes:

    @GET ("/flex/schedules/rest/v1/json/flight/")
    fun getFlightData(@Query("carrierFSCode") carrierFSCode: String,
                      @Query("flightNumber")flightNumber:String,
                      @Query("departing") departing:String,
                      @Query("year") year:String,
                      @Query("month") month:String,
                      @Query("day") day:String,
                      @Query("codeType") codeType: String,
                      @Query("appID") appID: String,
                      @Query("appKey") appKey:String
    ): Call<RetrievedFlightData>