android 调用改进API时,调用返回类型必须参数化为Call< Foo>或Call&lt;?extends Foo&gt; Exception

rjjhvcjd  于 11个月前  发布在  Android
关注(0)|答案(6)|浏览(124)

下面是我使用的一些技巧

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

字符串
下面是我称之为改造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


请帮助我在这方面。提前感谢

dba5bblo

dba5bblo1#

这个问题已经很老了,但这个问题似乎时不时地会出现。我在使用Gradle 8.2.1com.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.** { *; }

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

9bfwbjaz

9bfwbjaz2#

您不需要仅为此目的降级您的Gradle版本。当您更新到Gradle 8(或高于8的版本)时,R8将自动启用完整模式。如果您希望恢复到精简模式,只需在您的gradle.properties文件中添加以下行:
设置为“否”
编码愉快!

toiithl6

toiithl63#

我在发布模式下也有同样的错误,在升级模式下没有错误。将Gradle从8. 0. 0降级到7. 4. 0解决了我的问题。

zvms9eto

zvms9eto4#

不确定性

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

字符串

调用Retrofit API

在ResponseBody中获取response,然后将其解析为List of Object。

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

接口

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

pbwdgjma

pbwdgjma5#

我在项目中重命名包后遇到了同样的错误。除了启用缩小的签名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可能已经删除了我的类,因为包名不匹配,现在被阻止了。

goucqfw6

goucqfw66#

我也有这个错误.一切正常,除了签署APK的启用缩小.我已经通过添加一些规则到proguard-rules.pro文件修复它.一般来说,对于翻新,你需要添加以下规则到proguard-rules.pro:

-keepattributes Signature, InnerClasses, EnclosingMethod
-keepattributes RuntimeVisibleAnnotations, RuntimeVisibleParameterAnnotations
-keepattributes AnnotationDefault
-keepclassmembers,allowshrinking,allowobfuscation interface * {
    @retrofit2.http.* <methods>;
}
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement
-dontwarn javax.annotation.**
-dontwarn kotlin.Unit
-dontwarn retrofit2.KotlinExtensions
-dontwarn retrofit2.KotlinExtensions$*
-if interface * { @retrofit2.http.* <methods>; }
-keep,allowobfuscation interface <1>
-if interface * { @retrofit2.http.* <methods>; }
-keep,allowobfuscation interface * extends <1>
-keep,allowobfuscation,allowshrinking class kotlin.coroutines.Continuation
-if interface * { @retrofit2.http.* public *** *(...); }
-keep,allowoptimization,allowshrinking,allowobfuscation class <3>
-keep,allowobfuscation,allowshrinking class retrofit2.Response

字符串

相关问题