Retrofit 2 tutorial de ejemplo pero el error de visualización de GsonConverterFactory " No puede resolver el símbolo"


Estoy tratando de seguir el tutorial de Retrofit 2 , pero en esta parte del código hay un GsonConverterFactory que muestra 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);
    }
}

Previamente agregué en mi gradle.construir, no estoy seguro de si debo agregar GSON ya que dicen Retrofit 1.9 lo tiene, pero no se menciona nada sobre Retrofit 2:

dependencies {  
    // Retrofit & OkHttp
    compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
}
Author: StackOverflower, 2015-10-23

7 answers

EDITAR

Retrofit 2 ahora es estable. Use

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

En la sección de dependencias build.gradle

respuesta antigua

Con Retrofit 2.0 tienes que declarar en tu compilación.gradle la fábrica de conversión que desea utilizar. Añadir

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

A tu gradle y sincronízalo de nuevo

 61
Author: Blackbelt,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-10-26 11:20:23

De otro artículo en ese sitio

Retrofit 2 no se envía con Gson de forma predeterminada. Antes, no tenía que preocuparse por ningún convertidor integrado y podía usar Gson desde el primer momento. Este cambio de biblioteca afecta a su aplicación y también necesita importar un convertidor como un paquete hermano. Tocaremos el convertidor más adelante dentro de esta publicación y le mostraremos cómo configurar el Gson o cualquier otro convertidor de respuesta para su aplicación.

Por lo tanto, añadir esto a su build.gradle

dependencies {  
    compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
}
 11
Author: Andrew Brooke,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-10-23 14:14:09

La razón de tal comportamiento en mi caso fue el error tipográfico en build.gradle dependencia. Después del lanzamiento de beta4 he actualizado desde:

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

A

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

Y la dependencia correcta era

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


También vale la pena notar que beta4-retrofit no funcionará con beta2-gson !

 4
Author: Nikita Barishok,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-01 11:29:48

Como Biblioteca de Actualización Actualizada con Nueva Versión

Con

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

Debe incluir la dependencia de:

compile 'com.squareup.retrofit2:converter-gson:2.0.2'
 3
Author: Pratik Butani,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-04-25 06:41:39

Ya está disponible una nueva versión

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
 3
Author: Denys Milano,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-23 19:44:41

He utilizado

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

    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 y gson del mismo código de versión

 1
Author: Nilesh Panchal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-24 07:10:26
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'

Usa esto

 1
Author: Keshav Gera,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-18 05:04:44