【问题标题】:java.lang.IllegalArgumentException: Unable to create call adapter for classjava.lang.IllegalArgumentException:无法为类创建调用适配器
【发布时间】:2020-09-30 17:55:54
【问题描述】:

我尝试将 Retrofit 与 invoke() 方法一起使用,但发生错误

    suspend {
        Retrofit.Builder()
            .baseUrl("https://api.openweathermap.org/data/2.5/")
            .addConverterFactory(GsonConverterFactory.create())
            .build()
            .create(WeatherApi::class.java)
            .getPost("-33.8523341", "151.2106085", "***********API**********")

    }.invoke()

getPost() 方法

@GET("onecall?units=metric")
fun getPost(
    @Query("lat") lat: String,
    @Query("lon") lon: String,
    @Query("APPID") app_id: String
): WeatherData

错误

java.lang.IllegalArgumentException: Unable to create call adapter for 
                      class com.example.weatherapp.data.responses.WeatherData
                      for method WeatherApi.getPost

【问题讨论】:

  • 使用 Call 或 Deferred。然而,Deferred 需要 kotlin.coroutines 库,但它减少了样板代码。

标签: android kotlin retrofit2


【解决方案1】:
@GET("onecall?units=metric")
suspend fun getPost(               //use suspend
    @Query("lat") lat: String,
    @Query("lon") lon: String,
    @Query("APPID") app_id: String
): WeatherData

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-21
    • 1970-01-01
    • 2021-06-26
    • 2017-09-12
    • 2015-11-22
    • 1970-01-01
    • 2019-07-25
    • 1970-01-01
    相关资源
    最近更新 更多