【问题标题】:How to call enqueue callback?如何调用入队回调?
【发布时间】:2020-07-05 10:50:51
【问题描述】:

我尝试使用入队回调,但它显示为“类型不匹配”之类的错误。

必需:回调>"

这正是我写的。

fun searchLocation(searchString: String){
    showProgress.value = true

    val retrofit = Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create())
        .build()

    val service = retrofit.create(WeatherNetwork::class.java)
    service.getLocation(searchString).enqueue(object : Callback<List<Location>>{
        override fun onFailure(call: Call<List<Location>>, t: Throwable) {
        }
        override fun onResponse(
            call: Call<List<Location>>,
            response: Response<List<Location>>
        ) {
         }
    })
}

May be it'll be more clear with the pic

UPD:天气网络

 const val BASE_URL = "/My API/"

 interface WeatherNetwork{

 @GET ("search?")
 fun getLocation(SearchString : String) : List<Location>
}

【问题讨论】:

  • 确保你已经导入了 Retrofit Callback 接口,而不是其他同名的接口。
  • 我确实做到了,没有任何效果,仍然显示此错误:(
  • 可以添加接口WeatherNetwork吗?我猜返回可能不是Call&lt;List&lt;Location&gt;&gt;

标签: kotlin retrofit2


【解决方案1】:

你的WeatherNetwork错了,你的api调用需要返回一个Call

改变:

@GET ("search?")
 fun getLocation(SearchString : String) : List<Location>
}

@GET ("search?")
 fun getLocation(SearchString : String) : Call<List<Location>>
}

旁注:

我认为您需要更改您的 @GET("search?") 才能使其正常工作。

例如,如果您的 REST URI 是:localhost/search?st="wikipedia"

您需要将函数更新为:

@GET ("search")
 fun getLocation(@Query("st") SearchString : String) : List<Location>
}

看看:Retrofit 2 - URL Query Parameter

【讨论】:

  • 非常感谢!我试试这个!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-02
  • 1970-01-01
  • 2016-01-29
  • 2014-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多