调用返回类型必须参数化为 Call<Foo> 或 Call<? 调用改造 API 时扩展 Foo> 异常

Nav*_*een 15 android retrofit android-recyclerview

以下是我使用过的依赖项

implementation 'com.squareup.retrofit2:retrofit:2.0.2'
implementation 'com.squareup.retrofit2:converter-gson:2.0.2'
Run Code Online (Sandbox Code Playgroud)

下面是我调用改造 API 的代码

RequestBody jsonBody = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),(jsonInput));

            RetrofitAPI cashMemoService = RetrofitAPICLient.getClient().create(RetrofitAPI.class);
            Call<List<CashMemoDetails>> call = cashMemoService.getCashMemoPendingListObj(jsonBody);
Run Code Online (Sandbox Code Playgroud)

这是 RetrofitAPICLient

public static Retrofit getClient(){
        if(retrofit == null){
            retrofit = new Retrofit.Builder()
                    .baseUrl(newBseURL)
                    .client(okHttpClient)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }

        return retrofit;
    }
Run Code Online (Sandbox Code Playgroud)

下面是界面

@POST("GetCashMemoPunchingList")
    Call<List<CashMemoDetails>> getCashMemoPendingListObj(@Body RequestBody userData);
Run Code Online (Sandbox Code Playgroud)

以下是我得到的异常

 2020-10-20 10:59:47.320 27155-27155/ W/com.hpcl.gsa2: Accessing hidden method Ldalvik/system/CloseGuard;->warnIfOpen()V (greylist,core-platform-api, reflection, allowed)
    2020-10-20 10:59:47.335 27155-27155/ W/System.err: java.lang.IllegalArgumentException: Unable to create call adapter for interface g.g
   55-27155/ W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
    2020-10-20 10:59:47.336 27155-27155/ W/System.err: Caused by: java.lang.IllegalArgumentException: Call return type must be parameterized as Call<Foo> or Call<? extends Foo>
    2020-10-20 10:59:47.336 27155-27155/ W/System.err:  ... 22 more
Run Code Online (Sandbox Code Playgroud)

请帮助我。提前致谢

ava*_*cha 18

这个问题很老了,但问题似乎时不时就会出现。我也遇到了这个错误,并且使用Gradle 8.2.1andcom.android.tools.build:gradle 8.0.2降级来com.android.tools.build:gradle 7.4.2修复它!但降级并不是真正的解决方案,所以我进一步研究了一些

我可以通过从这里添加一些 Proguard 规则来修复它(nameley Retrofit 和 Okhttp 规则)

-keep class retrofit2.** { *; }
-keepattributes *Annotation*
-keep class com.squareup.okhttp.** { *; }
-keep interface com.squareup.okhttp.** { *; }
-keep class okhttp3.** { *; }
-keep interface okhttp3.** { *; }
Run Code Online (Sandbox Code Playgroud)

您还应该确保您的 Retrofit 和 Okhttp 版本是最新的,并且遵循自述文件中列出的 proguard / R8 配置提示

  • 这对我有用,似乎最新版本的 AGP R8 实际上混淆了所需的改造内容。 (2认同)

Nir*_*han 9

您无需仅出于此目的降级 Gradle 版本。当您更新到 Gradle 8(或高于 8 的版本)时,R8 将自动启用完整模式。如果您希望恢复到紧凑模式,只需将以下行添加到您的 gradle.properties 文件中:

android.enableR8.fullMode=false

快乐编码!


Ham*_*rar 2

依赖关系

implementation "com.squareup.okhttp3:okhttp:4.9.0"
implementation 'com.squareup.okhttp3:logging-interceptor:4.7.2'
Run Code Online (Sandbox Code Playgroud)

调用改造API

在 ResponseBody 中获取响应,然后将其解析为 Object 列表。

RetrofitAPI cashMemoService = RetrofitAPICLient.getClient().create(RetrofitAPI.class);
Call<ResponseBody> call = cashMemoService.getCashMemoPendingListObj(jsonBody);
Run Code Online (Sandbox Code Playgroud)

界面

@POST("GetCashMemoPunchingList")
Call<ResponseBody> getCashMemoPendingListObj(@Body RequestBody userData);
Run Code Online (Sandbox Code Playgroud)