gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 21:53:32 26 4
gpt4 key购买 nike

我正在尝试关注 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,因为他们说 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 并再次同步

关于android - Retrofit 2示例教程但GsonConverterFactory显示错误 "Cannot resolve symbol",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33304420/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com