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

AEM*_*iji 56 android gson retrofit

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

编译'com.squareup.retrofit:retrofit:2.0.0-
beta2'compile'c​​om.squareup.retrofit:converter-gson:2.0.0-beta1'

我有一个班级,我将通过改造访问我的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);
    }
Run Code Online (Sandbox Code Playgroud)

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

错误:(24,17)错误:类Builder中的方法addConverterFactory无法应用于给定类型; 必需:工厂发现:GsonConverterFactory原因:实际参数GsonConverterFactory无法通过方法调用转换转换为Factory

我怎么解决这个问题?我按照文档.怎么了?

Ily*_*kov 148

尝试使用相同的版本进行改造和转换器 - 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'
Run Code Online (Sandbox Code Playgroud)

重要的提示!

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'
Run Code Online (Sandbox Code Playgroud)

  • 你是个大个子)。但是没有任何资源表明我必须使用相同的beta2版本。 (2认同)

Coo*_*ind 7

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

implementation 'com.google.code.gson:gson:2.8.2'
Run Code Online (Sandbox Code Playgroud)

写:

implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
Run Code Online (Sandbox Code Playgroud)