Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Deferred retrofit request response problem

I want to do an async-await to my Retrofit request.

@GET("{companyId}/{filename}")
    @Streaming
    suspend fun getFilePart(
        @Path(value = "companyId") companyId: Int,
        @Path(value = "filename") filename: String,
        @QueryMap queryMap: Map<String, String>
    ): Deferred<ResponseBody>

and when i call it from a CoroutineScope i have

    val deferredList = pendingFiles.map {
        async(Dispatchers.IO) {
             try {
                 // runs in parallel in background thread
                 // Consider i have the data parameters....
                 apiManager.mApiService(base).getFilePart(it.companyId, fileName, urlQueryMap)
             } catch (e: Exception) {
                 e.printStackTrace()
             }
        }
     }

So the request returns 200 which means that it is successful, and i see the Logging with the File part data. But i get the following exception

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

W/System.err: java.lang.RuntimeException: Unable to invoke no-args constructor for com.google.firebase.inject.Deferred<okhttp3.ResponseBody>. Registering an InstanceCreator with Gson for this type may fix this problem.
W/System.err:     at com.google.gson.internal.ConstructorConstructor$14.construct(ConstructorConstructor.java:228)
W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:212)
W/System.err:     at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:40)
W/System.err:     at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:27)
W/System.err:     at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:243)
W/System.err:     at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:153)
W/System.err:     at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:520)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
W/System.err:     at java.lang.Thread.run(Thread.java:920)
W/System.err: Caused by: java.lang.UnsupportedOperationException: Interface can't be instantiated! Interface name: com.google.firebase.inject.Deferred
W/System.err:     at com.google.gson.internal.UnsafeAllocator.assertInstantiable(UnsafeAllocator.java:117)
W/System.err:     at com.google.gson.internal.UnsafeAllocator$1.newInstance(UnsafeAllocator.java:49)
W/System.err:     at com.google.gson.internal.ConstructorConstructor$14.construct(ConstructorConstructor.java:225)
W/System.err:   ... 9 more

>Solution :

If your function is suspend, it shouldn’t return Deferred. Just declare it this way:

@GET("{companyId}/{filename}")
@Streaming
suspend fun getFilePart(
    @Path(value = "companyId") companyId: Int,
    @Path(value = "filename") filename: String,
    @QueryMap queryMap: Map<String, String>
): ResponseBody // no Deferred here

Functions with suspend keyword are used like regular functions (that’s the beauty of coroutines), so they return a regular type. You actually start asynchronous code when you use coroutine builders like you do with async – this is the one that returns Deferred<T> and is not suspending.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading