在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()
        . Url(getUrl())
        .client(okHttpClient)
        //.addConverterFactory(GsonConverterFactory.create())
        .addConverterFactory(GsonConverterFactory.create())
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        .build();

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

                               


 

                    

收藏 打印