According to Kotlin Extensions - async/await test, to use async web request i should encapsulate the call inside a coroutine, like the example below, taken from the test:
private fun myGetAsync(url: String) = GlobalScope.async {
try {
Ion.with(App.instance.applicationContext)
.load(url)
.asString()
.await()
}
catch (e: Exception) {
Log.e(TAG,"Fail",e)
}
}
The problem is when i try to call the coroutine it, like this:
runBlocking { // but this expression blocks the main thread
Log.d(TAG,"RESULT" + myGetAsync("https://somewhere.earth/notExists").await())
}
i get the following library error:
java.lang.NoSuchMethodError: No interface method setCallback(Lcom/koushikdutta/async/future/FutureCallback;)Lcom/koushikdutta/async/future/Future; in class Lcom/koushikdutta/async/future/Future; or its super classes (declaration of 'com.koushikdutta.async.future.Future' appears in /data/app/com.byte_artisan.mchat2.qua-Rd32X2D4BZg92hn423sVxA==/base.apk!classes4.dex)
at com.koushikdutta.ion.IonRequestBuilder.resolveAndLoadRequest(IonRequestBuilder.java:383)
at com.koushikdutta.ion.IonRequestBuilder.getLoaderEmitter(IonRequestBuilder.java:347)
at com.koushikdutta.ion.IonRequestBuilder.getLoaderEmitter(IonRequestBuilder.java:310)
at com.koushikdutta.ion.IonRequestBuilder.execute(IonRequestBuilder.java:675)
at com.koushikdutta.ion.IonRequestBuilder.execute(IonRequestBuilder.java:636)
at com.koushikdutta.ion.IonRequestBuilder.asString(IonRequestBuilder.java:701)
at com.byte_artisan.mchat2.App$myGetAsync$1.invokeSuspend(App.kt:43)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:241)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:740)
How am i suppose to use it?
i was using an old version of com.koushikdutta.ion, incompatible with androidasync-kotlin library extension. I fixed by setting ion libraries to these versions:
implementation 'com.koushikdutta.ion:ion:3.0.8'
implementation 'com.koushikdutta.async:androidasync-kotlin:3.0.9'