在Android上的Scala代码中改进HTTP报告"HTTP方法注释是必需的"

drh*_*rhr 1 java android scala retrofit

我遇到了一个问题,其中Retrofit库正在识别方法而不是它的注释.它报告了上面标题中的错误消息.

(背景:我正在使用SBT sbt-android-plugin和Retrofit 1.6.1)

我的代码是这样的:

private trait MyService {
    @GET("/api/test")
    def test(): Observable[Any]
}

object MyService {
    private val restAdapter = new RestAdapter.Builder().setEndpoint("http://somewhere").build()
    val service = restAdapter.create(classOf[MyService])

    service.test().subscribe(/* you get the idea */) // This line throws a RuntimeException with message in title above
}
Run Code Online (Sandbox Code Playgroud)

GitHub周围散布着一个非常类似的例子,显然来自Reactive原则课程,例如这里.我只想说,我的设置是一样的.

当我单步执行时RestMethodInfo.parseMethodAnnotations(),requestMethod变量确实为null,requestTypeSIMPLE.如果我将@FormUrlEncoded注释添加到我的测试方法,requestType仍然设置为SIMPLE.

关于可能导致这种情况的任何想法?

drh*_*rhr 5

事实证明,Proguard正在剥离注释.

这个GitHub问题的建议很有帮助:

-keepattributes *Annotation*
-keep class retrofit.** { *; }
-keepclasseswithmembers class * {
@retrofit.http.* <methods>; }
-keepattributes Signature
Run Code Online (Sandbox Code Playgroud)

SO帖子中也提到了这个问题.