Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Retrofit2 可以改装将@Path参数从自定义类转换为基本类型_Retrofit2 - Fatal编程技术网

Retrofit2 可以改装将@Path参数从自定义类转换为基本类型

Retrofit2 可以改装将@Path参数从自定义类转换为基本类型,retrofit2,Retrofit2,我有以下改装请求: @GET("event/{id}") suspend fun getEvent(@Path("id") eventId: Long): Response<Event> 是否可以将EventId类作为路径参数传递,并告诉改型如何将值转换为Long? 这是我想要的方法签名: @GET("event/{id}") suspend fun getEvent(@Path("id") eventId: EventId): Response<Event> @GE

我有以下改装请求:

@GET("event/{id}")
suspend fun getEvent(@Path("id") eventId: Long): Response<Event>
是否可以将
EventId
类作为路径参数传递,并告诉改型如何将值转换为
Long
? 这是我想要的方法签名:

@GET("event/{id}")
suspend fun getEvent(@Path("id") eventId: EventId): Response<Event>
@GET(“事件/{id}”)
suspend fun getEvent(@Path(“id”)eventId:eventId):响应

我找到的解决方案是在改装
生成器中注册转换器工厂

class EventIdConverterFactory : Converter.Factory() {
    override fun stringConverter(type: Type, annotations: Array<Annotation>, retrofit: Retrofit) =
        if (type == EventId::class.java) {
            Converter<Any, String> { (it as EventId).value.toString() }
        } else {
            super.stringConverter(type, annotations, retrofit)
        }
}
class EventIdConverterFactory:Converter.Factory(){
替代stringConverter(类型:类型,注释:阵列,改装:改装)=
if(type==EventId::class.java){
转换器{(它作为EventId).value.toString()}
}否则{
super.stringConverter(类型、注释、改装)
}
}
class EventIdConverterFactory : Converter.Factory() {
    override fun stringConverter(type: Type, annotations: Array<Annotation>, retrofit: Retrofit) =
        if (type == EventId::class.java) {
            Converter<Any, String> { (it as EventId).value.toString() }
        } else {
            super.stringConverter(type, annotations, retrofit)
        }
}