改装转换器工厂无法访问 GsonConverterFactory

retrofit convertor factory can not access GsonConverterFactory

我已将这些依赖项包含到我的项目中:

compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta1'

我有一个 class,我将在其中通过改装访问我的 api:

 public static  <S> S createService(Class<S> serviceClass, String baseUrl) {


        Retrofit builder = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();    

            RestAdapter adapter = builder.build();*/

        return  builder.create(serviceClass);
    }

现在,它给我这个编译时错误:

Error:(24, 17) error: method addConverterFactory in class Builder cannot be applied to given types; required: Factory found: GsonConverterFactory reason: actual argument GsonConverterFactory cannot be converted to Factory by method invocation conversion

我该如何解决这个问题?我遵循了文档。怎么了?

尝试使用相同的版本进行改造和 converter-gson - 2.0.0-beta2。您正在使用 beta2 进行改造,beta1 进行转换器。

implementation 'com.squareup.retrofit:retrofit:2.0.0-beta2'
implementation 'com.squareup.retrofit:converter-gson:2.0.0-beta2'

重要提示!

2.0.0-beta3 版本以来,Retrofit 更改了其包名称。现在你应该使用 com.squareup.retrofit2。这是示例:

implementation 'com.squareup.retrofit2:retrofit:2.2.0'
implementation 'com.squareup.retrofit2:converter-gson:2.2.0'
  error: method addConverterFactory in class Builder cannot be applied to given types;
    required: Factory
    found: GsonConverterFactory
    reason: actual argument GsonConverterFactory cannot be converted to Factory by method invocation conversion

如果您收到此错误,原因是包含了错误的依赖项。

在应用程序 build.gradle 文件中添加/更改依赖项为

compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'  
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'

确保转换器版本是 2.0.0-beta2 而不是 2.0.0-beta1

对于最新的 Beta 2.0.3 版本,您需要添加:

compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'

确保改装库版本与 gson 转换器版本匹配。

这是最新的:

compile 'com.squareup.retrofit2:retrofit:2.0.0'
compile 'com.squareup.retrofit2:converter-gson:2.0.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0'

如果您使用测试版:

compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta2'

build.gradle(应用程序)中而不是:

implementation 'com.google.code.gson:gson:2.8.2'

写:

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