【问题标题】:How to get method's return type in OkHttp Interceptor?如何在 OkHttp 拦截器中获取方法的返回类型?
【发布时间】:2020-11-15 09:58:27
【问题描述】:

通过okhttp3.Request#tag(Invocation::class.java),我们可以得到methodNameannotations等。有没有什么方法可以使用Invocation类获取方法的返回类型。

我试过了

  • request.tag(Invocation::class.java)!!.method().returnType
  • request.tag(Invocation::class.java)!!.method().genericReturnType

但他们都只是返回class java.lang.Object,即使它是List

预期输出

@GET("products")
suspend fun getProducts(): List<Product>  // I need to get at least `List` as a String for this method

@GET("product")
suspend fun getProduct(): Product // I need to get at least `Object` as a String for this method

【问题讨论】:

    标签: java android kotlin retrofit okhttp


    【解决方案1】:

    这是一个挂起函数,所以 java.lang reflect.Method 的返回类型不是你声明的返回类型。查看反编译的字节码,您将看到 Java 方法以及声明的返回类型在签名中的最终位置。

    【讨论】:

    • 好的,我在签名字段字符串中看到了返回类型。现在,我怎样才能在代码中获取返回类型,它是一个私有字段? i.imgur.com/AFKDM45.png@Jesse
    • 在反射类方法上使用反射? :-|
    猜你喜欢
    • 1970-01-01
    • 2013-05-15
    • 2016-04-13
    • 2015-12-16
    • 1970-01-01
    • 2019-10-28
    • 2019-03-03
    • 2020-09-18
    • 2018-05-17
    相关资源
    最近更新 更多