调用retrofit API时,调用返回类型必须参数化为Call<Foo>或Call<? extends Foo>异常

问题描述 投票:0回答:5

以下是我使用过的依赖项

implementation 'com.squareup.retrofit2:retrofit:2.0.2'
implementation 'com.squareup.retrofit2:converter-gson:2.0.2'

下面是我调用retrofit 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);

这是RetrofitAPICLient

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

        return retrofit;
    }

以下是界面

@POST("GetCashMemoPunchingList")
    Call<List<CashMemoDetails>> getCashMemoPendingListObj(@Body RequestBody userData);

以下是我遇到的异常

 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

请帮助我。提前致谢

android android-recyclerview retrofit
5个回答
11
投票

这个问题很老了,但问题似乎时不时就会出现。我使用

Gradle 8.2.1
com.android.tools.build:gradle 8.0.2
也遇到了这个错误,降级到
com.android.tools.build:gradle 7.4.2
修复了它!但降级并不是真正的解决方案,所以我进一步调查了

我能够通过添加一些来自here的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.** { *; }

您还应该确保您的 Retrofit 和 Okhttp 版本是最新的,并且遵循 自述文件

中列出的 proguard / R8 配置提示

4
投票

我在发布模式下遇到了同样的错误,在调试模式下没有错误。将 Gradle 从 8.0.0 降级到 7.4.0 解决了我的问题。


4
投票

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

android.enableR8.fullMode=false

编码快乐!


2
投票

依赖关系

implementation "com.squareup.okhttp3:okhttp:4.9.0"
implementation 'com.squareup.okhttp3:logging-interceptor:4.7.2'

调用Retrofit API

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

RetrofitAPI cashMemoService = RetrofitAPICLient.getClient().create(RetrofitAPI.class);
Call<ResponseBody> call = cashMemoService.getCashMemoPendingListObj(jsonBody);

界面

@POST("GetCashMemoPunchingList")
Call<ResponseBody> getCashMemoPendingListObj(@Body RequestBody userData);

0
投票

在我的项目中重命名包后,我遇到了同样的错误。除了启用了 minify 的签名 APK 之外,一切工作正常。我已经通过将其添加到我的 proguard-rules.pro 来修复它:

-keep class retrofit2.** { *; }
-keepattributes *Annotation*
-keep class com.squareup.okhttp.** { *; }
-keep interface com.squareup.okhttp.** { *; }
-keep class okhttp3.** { *; }
-keep interface okhttp3.** { *; }
-keep class com.yourproject.app.** { *; }

我想问题出在最后一行。由于包名称不匹配,minifier 可能已经删除了我的类,现在已经阻止了。

© www.soinside.com 2019 - 2024. All rights reserved.