[英]Retrofit "HTTP method annotation is required"
嘗試使用 Coroutines 和 Jetpack Compose,並從 Retrofit 獲得無用的堆棧跟蹤:
java.lang.IllegalArgumentException: HTTP method annotation is required (e.g., @GET, @POST, etc.).
for method PokeApi.getPokeList
改造調用API(導入都是2.7.1):
import retrofit2.http.GET
import retrofit2.http.Path
import retrofit2.http.Query
interface PokeApi {
@GET("pokemon")
suspend fun getPokeList(@Query("limit") limit: Int = 151
): PokeListRequest
@GET("pokemon/{species}")
suspend fun getPokemon(@Path("species") pokemon: String
): PokeRequest
}
網絡來電:
class PokeClient {
private var gson = Gson()
private var caller = Retrofit.Builder()
.baseUrl("https://pokeapi.co/api/v2/")
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
.create(PokeApi::class.java)
suspend fun getAllPokemon(): PokeListRequest {
return caller.getPokeList()
}
suspend fun lookupPokemon(species: String): PokeRequest {
return caller.getPokemon(species)
}
}
從 ViewModel 調用:
fun retrieveList() {
viewModelScope.launch {
PokeClient().getAllPokemon().results.map{it.name}
.also{pokeList.value = it}
}
}
}
它不漂亮,但我不明白為什么它不應該工作。
將 Retrofit 調用者分離到不同的模塊中解決了這個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.