【发布时间】:2018-01-08 23:17:57
【问题描述】:
给定以下Retrofit接口:
@GET("offices")
fun getOffices(@Query("uid") uid: String,
@Query("lat") latitude: Double,
@Query("lon") longitude: Double
): Call<List<Office>>
如何用更友好的GeoLocation 类型替换位置参数...
data class GeoLocation(
val latitude: Double,
val longitude: Double
)
...在请求时自动转换为lat和lon,例如:
@GET("offices")
fun getOffices(@Query("uid") uid: String,
@Query("location") location: GeoLocation
): Call<List<Office>>
这是改造设置:
fun createRetrofit(baseUrl: String,
okHttpClient: OkHttpClient): Retrofit {
val moshi = Moshi.Builder()
.build()
return Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(MoshiConverterFactory.create(moshi))
.client(okHttpClient)
.build()
}
【问题讨论】:
-
您可以使用接口默认方法并委托给较旧的方法,只需我的 2 美分。
-
@KirillRakhman 谢谢你的链接。我不明白的是如何使用
Converter<F, T>将GeoLocation对象转换为lat和lonDouble 值。 -
目前看来这是不可能的。