Retrofit 2示例教程但是GsonConverterFactory显示错误"Cannot resolve symbol"

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

我正在尝试遵循 Retrofit 的 2 tutorial, 但是在这部分代码中有一个 GsonConverterFactory 显示错误 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);
    }
}

之前我在 gradle.build 中添加了 GSON,我不确定是否应该添加 GSON,因为他们说 Retrofit 1.9 有它,但没有提到 Retrofit 2:

dependencies {  
    // Retrofit & OkHttp
    compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
}

编辑

改造 2 现已稳定。使用

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

在您的 build.gradle 依赖项部分

旧答案

对于 Retrofit 2.0,您必须在 build.gradle 中声明您要使用的转换工厂。添加

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

到您的 gradle 并再次同步

来自该站点的another article

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.

因此,将此添加到您的 build.gradle

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

在我的案例中出现这种行为的原因是 build.gradle 依赖项中的拼写错误。 beta4 发布后我更新了:

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


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

并且正确的依赖项是

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


同样值得注意的是,beta4 - 改造不适用于 beta2 - gson!

使用新版本更新改造库

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

您必须包含以下依赖项:

compile 'com.squareup.retrofit2:converter-gson:2.0.2'

新版本现已推出

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

我用过

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 and gson of the same version code
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'

使用这个