Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

retrofit convertor factory can not access GsonConverterFactory

I have included these dependencies to my project:

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

I have a class where I am going to access my api's via retrofit:

 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);     } 

And now, it gives me this compile time error :

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

How can I solve this? I followed the documentation. What is wrong?

like image 765
AEMLoviji Avatar asked Oct 02 '15 07:10

AEMLoviji


2 Answers

Try to use same version for retrofit and converter-gson - 2.0.0-beta2. You are using beta2 for retrofit and beta1 for converter.

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

Important note!

Retrofit change its package name since 2.0.0-beta3 version. Now you should use com.squareup.retrofit2. Here is example:

implementation 'com.squareup.retrofit2:retrofit:2.2.0' implementation 'com.squareup.retrofit2:converter-gson:2.2.0' 
like image 147
Ilya Tretyakov Avatar answered Sep 23 '22 06:09

Ilya Tretyakov


In build.gradle (app) instead of:

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

write:

implementation 'com.squareup.retrofit2:converter-gson:2.3.0' 
like image 26
CoolMind Avatar answered Sep 21 '22 06:09

CoolMind