【问题标题】:retrofit convertor factory can not access GsonConverterFactory改造转换器工厂无法访问 GsonConverterFactory
【发布时间】:2015-12-30 08:50:09
【问题描述】:

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

编译'com.squareup.retrofit:retrofit:2.0.0-beta2'
编译'com.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);
    }

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

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

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

【问题讨论】:

    标签: android gson retrofit


    【解决方案1】:

    尝试使用相同的版本进行改造和转换器-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'
    

    重要提示!

    Retrofit 从2.0.0-beta3 版本开始更改其包名称。现在你应该使用com.squareup.retrofit2。这是一个例子:

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

    【讨论】:

    • 你是个大人物)。但是没有任何资源表明我必须使用相同的 beta2 版本。
    • @AEMLoviji 这个资源叫常识,你不同意吗?最值得注意的是,因为在 beta 阶段,界面可能会发生变化,这是在改造文档中编写的。
    • 我刚遇到这个问题。她解决了2小时。谢谢!)
    • 请考虑更新答案,因为从 Beta 2.0.3 开始,包名不再是 com.squareup.retrofit,而是 com.squareup.retrofit2
    【解决方案2】:

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

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

    写:

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

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

      • 很难发现差异。所以对于像我一样盲的每个人 - 你需要在包名中添加2
      【解决方案4】:

      这是最新的:

      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'
      

      【讨论】:

        【解决方案5】:
          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

        【讨论】:

          猜你喜欢
          • 2016-01-12
          • 1970-01-01
          • 1970-01-01
          • 2016-10-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-06-23
          相关资源
          最近更新 更多