Reputation: 61
I have problem parsing array of objects using Retrofit + RxJava
JSON contains only this array
{
"files": [
{
"id": 10,
"notificationId": 15,
"name": "cats.ncs",
"dateTime": "2019-01-07T17:34:45"
}
]
}
retrotfit service
@GET("/api/FileApi/files")
fun files(): Observable<FilesResponse>
where FilesResponse is
data class FilesResponse(
@SerializedName("files")
var files: List<FileElement>
)
and FileElement
data class FileElement(
@SerializedName("id")
var id: Long,
@SerializedName("notificationId")
var notificationId: Long,
@SerializedName("name")
var name: String,
@SerializedName("dateTime")
var dateTime: String
)
when I run it I get always
the return type of CallObjectMethodA does not match io.reactivex.Observable ApiService.files()
So how do I parse JSON containing only an array?
Upvotes: 1
Views: 1290
Reputation: 2143
Few notes
First of, you might need to show us how you call ApiService.files()
What could possibly went wrong
How it is called
What could be the possible solution
Invoke the method like this
ApiService
.files()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
// Handle the values here
},{
// Handle the error here
},{
// Handle the completion here (optional)
})
Not like this
ApiServices.files()
nor
var fileResponse = ApiServices.files()
Ensure the you have setup your retrofit builder well, as mentioned above
Other than that, without the code on how you call it, we won't be able to help you in an in-depth manner.
Upvotes: 0
Reputation: 373
Try using RxJava2 Adapter
Integration
implementation 'com.squareup.retrofit2:adapter-rxjava2:{retrofit_version}'
Retrofit client setup
new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) //option 1
.addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.newThread())) //option 2
.build();
Upvotes: 1