1

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?

1
  • 1
    Show how you are calling it Commented Jan 7, 2019 at 20:42

2 Answers 2

1

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();
Sign up to request clarification or add additional context in comments.

Comments

0

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

  • RxJava2CallAdapterFactory
  • GsonConverterFactory

Other than that, without the code on how you call it, we won't be able to help you in an in-depth manner.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.