Retrofit2 中使用@Body 时出现Unable to create @Body converter for class

本文解决了一个在Retrofit2中使用@Body注解时遇到的常见错误,详细介绍了如何通过调整依赖版本来解决java.lang.IllegalArgumentException异常,确保转换器与Retrofit版本匹配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Retrofit2中使用@Body注解时出现错误提示java.lang.IllegalArgumentException: Unable to create @Body converter for class xxxx (parameter #1),于是开始向百度谷歌求救,基本都是说要要添加转换器.addConverterFactory(GsonConverterFactory.create()),或者不能跟

@FormUrlEncoded一起使用,或者有加密的类容就要自定义转换器,

可是我明明是这样的,有没有加密,最后发现原来转换器要需要跟retrofit的版本对应,我原来的依赖是这样的

implementation 'io.reactivex.rxjava2:rxjava:2.1.0'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
implementation 'org.greenrobot:eventbus:3.0.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.3.1'

后来把转换器的依赖版本改成2.3.0,如下所示:

implementation 'com.squareup.retrofit2:converter-gson:2.3.0'

Retrofit中转换器的名称没有变,但是依赖路径变成这个了import retrofit2.converter.gson.GsonConverterFactory;

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(getUrl())
        .client(okHttpClient)
        //.addConverterFactory(GsonConverterFactory.create())
        .addConverterFactory(GsonConverterFactory.create())
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        .build();

总算可以了,心累,记录下来希望可已帮到有需要的人。

                               


 

                    

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值