-
-
Save Kashif-E/ff1dcec09f785318fe74eb6856cf3a2e to your computer and use it in GitHub Desktop.
override suspend fun doNetworkCallsInParallel() { | |
try { | |
CoroutineScope(Dispatchers.IO).launch { | |
val usNewsResponse = async { | |
apiServices.getHeadLines(country = "us") | |
} | |
val healthNewsResponse = async { | |
apiServices.getHeadLines(category = "health") } | |
val x = usNewsResponse.await() | |
val v = healthNewsResponse.await() | |
if (x.isSuccessful && v.isSuccessful){ | |
Log.e("US Response", x.body().toString()) | |
Log.e("Health", v.body().toString()) | |
} | |
} | |
} catch (e: Exception) { | |
Log.e("Exception", e.localizedMessage) | |
} | |
} | |
Happy coding <3 |
@shahwaiz90 I don't think so are you using retrofit?
isSuccessful is no longer accessible
@shahwaiz90 If you want to access Response object, check the return type of your ApiService function which is making the request. If the return type is of type Call, you need to use awaitResponse { } instead of await { } to get result as Response. Afterwards, you can access isSuccessful
Reference: https://youtu.be/S-10lLA0nbk
@Kashif-E hey can i call 2 api like 2 baseUrl with retrofit 2 and use the coroutines to get the response?
@seynth For that there are two ways, you either create two separate retrofit instances and use them to call APIs in parallel, or create separate instances of interceptors to handle two different base URLs, and passing them to retrofit at run time and making async calls
isSuccessful is no longer accessible