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

9

43

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'
}
Casillas answered 23/10, 2015 at 14:9 Comment(3)
You cannot use GsonConverterFactory in Retrofit 2, since it is only applicable to Retrofit 1Hindgut
I am following this tutorial : wiki.workassis.com/android-retrofit-2-1-http-clientShoreless
Refer to https://mcmap.net/q/390273/-custom-converter-for-retrofit-2Mummify
C
92

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

Civvies answered 23/10, 2015 at 14:13 Comment(0)
L
11

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'
}
Loggerhead answered 23/10, 2015 at 14:14 Comment(0)
A
5

New version is now available

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
Abnormal answered 23/6, 2016 at 19:44 Comment(0)
Z
4

The reason to such behavior in my case was typo in build.gradle dependency. After beta4 release I've updated from:

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

to

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

and the right dependency was

compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'


Also worth notice, that beta4 - retrofit won't work with beta2 - gson!

Zip answered 1/3, 2016 at 11:29 Comment(0)
F
4

As Updated Retrofit Library with New Version

With

compile 'com.squareup.retrofit2:retrofit:2.0.2'

You must have to include dependency of:

compile 'com.squareup.retrofit2:converter-gson:2.0.2'
Flatus answered 25/4, 2016 at 6:41 Comment(0)
B
3

I have used

RestService restService=new Retrofit.Builder()
                    .baseUrl(Constants.Base_URl)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .client(new OkHttpClient.Builder().readTimeout(60, TimeUnit.SECONDS).connectTimeout(60, TimeUnit.SECONDS).build())
                    .build().create(RestService.class);

And dependencies:

        compile 'com.squareup.retrofit2:retrofit:2.1.0'
        compile 'com.squareup.retrofit2:converter-gson:2.1.0'
        // RxJava adapter for retrofit
        compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
        // RxJava libraries
        compile 'io.reactivex:rxjava:1.0.10'
        compile 'io.reactivex:rxandroid:1.1.0'

use retrofit and gson of the same version code
Bub answered 24/9, 2016 at 7:10 Comment(1)
I think compile is deprecated; should replace with implementationUnderstrapper
D
2
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'

use this

Driskell answered 18/7, 2017 at 5:4 Comment(0)
K
0

As gson is by-default not available with retrofit, use both retrofit and converter-gson dependencies as:

def retrofitVersion = "2.9.0"
implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
implementation "com.squareup.retrofit2:converter-gson:$retrofitVersion"

You can find the latest version of retrofit here and gson converter here

Kinross answered 13/7, 2023 at 13:27 Comment(0)
L
0

You are not implementing the GsonConverterFactory . GsonConverterFactory is a used for converting JSON responses from the server into Kotlin or Java objects.

implementation("com.squareup.retrofit2:retrofit:2.9.0")
implementation("com.squareup.retrofit2:converter-gson:2.9.0")

please add this dependency into your build.gradle(app) .

Languor answered 14/3 at 10:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.