(改造)找不到类崩溃应用的转换器

2022-08-31 11:44:36

因此,最近发布了Retrofit 2.0.0,并且没有任何关于如何使用它的更新示例,但是我试图为基本的API调用实现它。我得到一个

java.lang.IllegalArgumentException: Unable to create converter for class` 

引起

Caused by: java.lang.IllegalArgumentException: Could not locate converter for class orbyt.app.dataclass. Tried:
* retrofit.OkHttpBodyConverterFactory

尝试进行 api 调用时。


答案 1

我遇到了同样的问题。我通过添加以下内容来修复它:

compile 'com.squareup.retrofit2:converter-gson:<latest-version>'

到我的 build.gradle

然后在创建我的改造实例时指定转换器。

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Constants.API_BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

答案 2

在改造 2.0 中,转换器不包含在软件包中,当您使用改造 2.0 时,请确保遵循新的 URL 模式

基本网址:始终以 / 结尾

@Url:不要以 / 开头

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(Constants.API_BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build();

有关 2.0 的更多信息,请访问此链接 改造 2.0:最大的更新

并更新build.gradle。

implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"

并在项目级 build.gradle 文件中添加扩展名

ext {
retrofit_version= "2.x.x"
}

推荐