Kotlin Retrofit2 排队 "Unresolved reference Enqueue"
Kotlin Retrofit2 Enqueue "Unresolved reference Enqueue"
您好,我收到一条未解决的引用入队消息,运行 没有可供解决的选项。
回调是在 Fragment 中的按钮 setOnCLickListener 中进行的。
其他建议将 Gradle 版本设置为 3.1.3(以前使用 3.6.1),但错误仍然存在。
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 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
)
}
和客户端实例
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
}
}
Client 实例正常工作,因为我进行了其他正常工作的调用。
另一个问题是我需要等待回复才能继续。我以前在另一个电话中使用过 newSingleThreadExecutor,但我知道这不是最佳做法。也将不胜感激。
谢谢
joncb
将您的 getFlightData()
更改为 return Call<RetrievedFlightData>
。于是就变成了:
@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>
您好,我收到一条未解决的引用入队消息,运行 没有可供解决的选项。
回调是在 Fragment 中的按钮 setOnCLickListener 中进行的。
其他建议将 Gradle 版本设置为 3.1.3(以前使用 3.6.1),但错误仍然存在。
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 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
)
}
和客户端实例
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
}
}
Client 实例正常工作,因为我进行了其他正常工作的调用。
另一个问题是我需要等待回复才能继续。我以前在另一个电话中使用过 newSingleThreadExecutor,但我知道这不是最佳做法。也将不胜感激。
谢谢
joncb
将您的 getFlightData()
更改为 return Call<RetrievedFlightData>
。于是就变成了:
@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>