使用协程和Jetpack进行实验,并从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}
}
}
}
虽然不漂亮,但我不明白为什么它不起作用。
答案 0 :(得分:0)
将Retrofit调用者分为不同的模块即可解决此问题。