只要没有实施额外的方法,Traits就可以很好地与Retrofit配合使用。根据返回类型抛出RetrofitError: TwitchApi.someMethod: HTTP method annotation is required (e.g., @GET, @POST, etc.).
或java.lang.IllegalArgumentException: TwitchApi.someMethod: Must have either a return type or Callback as last argument.
。
有没有办法让改造忽略一个没有用retrofit.http.GET / PUT / ...
任何一个注释的方法?
public trait SomeApi {
GET("/endpoint")
public fun getSomething(Query("user") user: String): Observable<SomeResponse>
class object {
public fun create(): SomeApi {
val restAdapter = RestAdapter.Builder().setEndpoint("http://localhost").build()
return restAdapter.create<TwitchApi>(javaClass<SomeApi >())
}
}
public fun someMethodThatBreaksRetrofit(user: String) : Int {
return processResponse(getSomething(user))
}
}
答案 0 :(得分:8)
你根本不应该这样做。幸运的是,在Kotlin你可以使用扩展功能
interface SomeApi {
GET("/endpoint")
fun getSomething(Query("user") user: String): Observable<SomeResponse>
}
fun SomeApi.someMethod(user : String) : Observable<Int>
= processResponse(getSomething(user))