Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit 2 example tutorial but GsonConverterFactory display error "Cannot resolve symbol"

I'm trying to follow Retrofit's 2 tutorial, but on this part of the code there is a GsonConverterFactory that displays error Cannot resolve symbol:

public class ServiceGenerator {      public static final String API_BASE_URL = "http://your.api-base.url";      private static OkHttpClient httpClient = new OkHttpClient();     private static Retrofit.Builder builder =             new Retrofit.Builder()                     .baseUrl(API_BASE_URL)                     //THIS IS THE LINE WITH ERROR!!!!!!!!!!!!                     .addConverterFactory(GsonConverterFactory.create());      public static <S> S createService(Class<S> serviceClass) {         Retrofit retrofit = builder.client(httpClient).build();         return retrofit.create(serviceClass);     } } 

Previously I added in my gradle.build, I'm not sure if I should add GSON since they say Retrofit 1.9 has it but nothing is mentioned about Retrofit 2:

dependencies {       // Retrofit & OkHttp     compile 'com.squareup.retrofit:retrofit:2.0.0-beta2' } 
like image 592
StackOverflower Avatar asked Oct 23 '15 14:10

StackOverflower


2 Answers

EDIT

retrofit 2 is now stable. Use

compile 'com.squareup.retrofit2:retrofit:2.3.0' compile 'com.squareup.retrofit2:converter-gson:2.3.0' 

in your build.gradle dependency section

old answer

with Retrofit 2.0 you have to declare in your build.gradle the convert factory you want to use. Add

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

to your gradle and sync it again

like image 155
Blackbelt Avatar answered Nov 01 '22 11:11

Blackbelt


From another article on that site

Retrofit 2 doesn’t ship with Gson by default. Before, you didn’t need to worry about any integrated converter and you could use Gson out of the box. This library change affects your app and you need to import a converter as a sibling package as well. We’ll touch the converter later within this post and show you how to config the Gson or any other response converter for your app.

Thus, add this to your build.gradle

dependencies {       compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2' } 
like image 40
Andrew Brooke Avatar answered Nov 01 '22 09:11

Andrew Brooke