【问题标题】:Retrofit HTTP reporting "HTTP method annotation is required" in Scala code on Android在 Android 上的 Scala 代码中改造 HTTP 报告“需要 HTTP 方法注释”
【发布时间】:2016-03-08 21:38:19
【问题描述】:

我遇到了一个问题,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
}

在 GitHub 上散布着一个非常相似的示例,显然来自响应式原理课程,例如here。可以这么说,我的设置是一样的。

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

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

【问题讨论】:

    标签: java android scala retrofit


    【解决方案1】:

    原来 Proguard 正在剥离注释。

    GitHub issue 中的建议很有帮助:

    -keepattributes *Annotation*
    -keep class retrofit.** { *; }
    -keepclasseswithmembers class * {
    @retrofit.http.* <methods>; }
    -keepattributes Signature
    

    而且这个问题在这个SO post中也有提到。

    【讨论】:

      【解决方案2】:

      在改造:2.0.0 库中需要使用来自retrofit2 的注解:

      import retrofit2.http.Field;
      import retrofit2.http.FormUrlEncoded;
      import retrofit2.http.GET;
      

      而不是retrofit

      import retrofit.http.Field;
      import retrofit.http.FormUrlEncoded;
      import retrofit.http.GET;
      

      【讨论】:

        猜你喜欢
        • 2020-05-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-21
        • 1970-01-01
        • 1970-01-01
        • 2014-02-16
        • 1970-01-01
        相关资源
        最近更新 更多