android - java.lang.IllegalArgumentException:无法为类创建@Body 转换器
问题描述
最近我从 GSONConverterFactory 切换到 MoshiConverterFactory。除了一个电话,一切都很好。像这里的其他 API 调用一样,我也在使用 @Body 注释,但我收到此错误 java.lang.IllegalArgumentException: Unable to create @Body converter for class
我的请求类:
data class DemoRequest(
val emailId: String? = null,
val demoData: List<DemoDomain?>? = null,
val userName: String? = null
)
这里还有一件事要提到的是,使用 GSONConverterFactory 它工作正常,但是当我切换到 MoshiConverterFactory 时,它会抛出错误。
改造版本 = '2.3.0'
服务接口:
@POST("call/api")
fun sendToServer(@Body request: DemoRequest):retrofit2.Call<RemoteResponse>
val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(MoshiConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
更新------------- 我在请求中发送 Date 对象,所以我需要使用自定义适配器,它现在工作正常
解决方案
您是否记得在构建 Retrofit 时更改为MoshiConverterFactory
?
Retrofit.Builder().baseUrl(...).addConverterFactory(MoshiConverterFactory.create()).build()
此外,Retrofit 的最新版本是 2.5.0,因此您可以尝试升级并确保您的转换器也是相同的版本。
推荐阅读
- date - SSAS 表格 - 事实表和第二个日期维度之间没有关系
- python - 获取 Spotify Artist URI(Python 初学者)
- python - Discord 机器人在特定输入上说出特定内容
- python - 如何从同一个 Excel 文件中的多个工作表中获取所选列的平均值
- algorithm - 我应该使用什么解决方案来生成所有可能的字母组合的列表?
- unity3d - Unity 3D 2D 导入精灵像素化
- javascript - JS,如何仅更改父元素
- python - 蟒蛇 | NameError:未定义名称“null”
- youtube-api - 使用 YouTube API,是否可以按上传日期过滤搜索?
- flutter - 颤振我如何改变水龙头上容器的颜色?