kotlin - 如何使用 Retrofit 和 RxJava 解析 json 数组
问题描述
我在使用 Retrofit + RxJava 解析对象数组时遇到问题
JSON 只包含这个数组
{
"files": [
{
"id": 10,
"notificationId": 15,
"name": "cats.ncs",
"dateTime": "2019-01-07T17:34:45"
}
]
}
改装服务
@GET("/api/FileApi/files")
fun files(): Observable<FilesResponse>
FilesResponse 在哪里
data class FilesResponse(
@SerializedName("files")
var files: List<FileElement>
)
和 FileElement
data class FileElement(
@SerializedName("id")
var id: Long,
@SerializedName("notificationId")
var notificationId: Long,
@SerializedName("name")
var name: String,
@SerializedName("dateTime")
var dateTime: String
)
当我运行它时,我总是
CallObjectMethodA 的返回类型与 io.reactivex.Observable ApiService.files() 不匹配
那么如何解析只包含一个数组的 JSON 呢?
解决方案
尝试使用RxJava2 适配器
一体化
implementation 'com.squareup.retrofit2:adapter-rxjava2:{retrofit_version}'
改造客户端设置
new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) //option 1
.addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.newThread())) //option 2
.build();
推荐阅读
- php - 我想我从 ReflectionProperty 中发现了一个带有 setValue 的错误
- cmake - CMake 的 -S 和 -H 选项有什么区别?
- c# - Asp.Net Core 允许 CORS 中的 IP 范围
- javascript - UL 标签中的展开和折叠
- angular - 使用带有 MSAL for Angular 的社交身份提供程序向我的 AD-B2C 注册用户
- pyspark - 如何在不使用 withcolumn 的情况下向数据框添加列
- java - 从我的 Centos 7 服务器发送邮件不起作用
- angular - 如何为 ngb 分页动态设置大小选择器输入
- javascript - 如何在Vuejs中显示帖子总数Wp rest api
- ruby-on-rails - 重新路由=> http://localhost:3000/today_attendance?date=2019-03-29 到 http://localhost:3000/today_attendance