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
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
CodePudding user response:
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.