繁体   English   中英

改造“需要HTTP方法注解”

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM